Soumya Gangamwar Soumya Gangamwar - 3 months ago 15
AngularJS Question

Please help to send checklist to service without changing the model value?

Please give some time to read this question and help me to solve my problem please.
I have a json array like

$scope.tablesData = [
{
"table_name": 'table1',
"charts": 0,
"flex_sheet": 0,
"dtf": 0
},{
"table_name": 'table2',
"charts": 1,
"flex_sheet": 1,
"dtf": 1
},{
"table_name": 'table3'

},{
"table_name": 'table4'

},
{
"table_name": 'table5'

}]


I have html page like this

<div ng-controller="MyCtrl">
<table class="table">
<thead class="table-inverse">
<tr>
<th>Table Name</th>
<th>Charts</th>
<th>FlexSheet</th>
<th>Dtf</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="table in tablesData">
<td>{{table.table_name}}</td>
<td><input type="checkbox" ng-true-value="1" ng-false-value='0' ng-model="table.charts"></td>
<td><input type="checkbox" ng-true-value="1" ng-false-value='0' ng-model="table.flex_sheet"></td>
<td><input type="checkbox" ng-true-value="1" ng-false-value='0' ng-model="table.dtf"></td>
</tr>
</tbody>
</table>

<input type="button" class="btn btn-info" value="submit" ng- click="upadateTableInfo()">

</div>


You can see this by http://jsfiddle.net/soumyagangamwar/gpm72kmo/

I have to send table data to service in submit click.
So I called 'updateTableInfo' method in ng-click.

Please observe data i.e $scope.tableData once. After controller is loading after getting data in view if I checked some values like rows 3,4,5(table3,4,5) then click submit button then controller receives the one array contain 5 objects each object contain 4 properties like table_name, charts, fles_sheet, dtf with thiere values. it's receives fine. my problem is after view loading If I did n't change any values(3,4,5) then in submit click I receive the array contain 5 objects but in that I receive 3,4,5 objects with out have properties charta, flex sheet, dtf. But I have a requirement If user not doing any thing then I have to send 0 to post method but object should contain all 4 properties.

Please help me to do this?

Answer

Just add the ng-init to set the modal values by default to 0 as follows. I hope it solves your problem.

 <tr ng-repeat="table in tablesData">
        <td>{{table.table_name}}</td>
        <td><input type="checkbox" ng-init="table.charts = 0"  ng-true-value="1" ng-false-value='0' ng-model="table.charts"></td>
        <td><input type="checkbox" ng-init="table.flex_sheet = 0" ng-true-value="1" ng-false-value='0'  ng-model="table.flex_sheet"></td>
        <td><input type="checkbox" ng-init="table.dtf = 0" ng-true-value="1" ng-false-value='0' ng-model="table.dtf"></td>
 </tr>

Check this fiddle: http://jsfiddle.net/gpm72kmo/1/