4

I have a list of object and iterating that list using ng-repeat to generate table row. each td contains source name and checkbox. I want to bind the checkbox with a property which is not available into list. how that is possible? The list is like that:-

scope.reasons = [
        {sourceName:'Lack of rainfall'},
        { sourceName: 'Change in land use' },
        {sourceName:'Change in Land Cover'}
        ];

and the HTML code is like that:-

<table>
  <tr ng-repeat="source in reasons">
    <td>{{source.sourceName}}</td>
    <td><input type="checkbox" ng-checked="source.postAdequate"></td>
  </tr>
</table>
4
  • if the property is not available in object, you want that object property to be added dynamically? Commented Jan 2, 2017 at 5:11
  • can you hard code some example like how the property should be? Commented Jan 2, 2017 at 5:13
  • @Mayank Nimje yes. Commented Jan 2, 2017 at 5:14
  • after checked and unchecked the check boxes the list should look like this- scope.reasons = [ {sourceName:'Lack of rainfall',postAdequate:true }, { sourceName: 'Change in land use',postAdequate:false }, {sourceName:'Change in Land Cover',postAdequate:true}] Commented Jan 2, 2017 at 5:18

4 Answers 4

1

You can do this using the ng-model attribute, ng-change is just for the checking purposes that is change detection.

<table>
    <tr ng-repeat="source in reasons">
      <td>{{source.sourceName}}</td>
      <td>
        <input type="checkbox" ng-model="source.postAdequate" ng-change="changeDet()">
      </td>
    </tr>
  </table>

Demo Fiddle

Hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

ng-change isn't doing anything related to question. Explanation doesn't make sense
if i use ng-model instead of ng-checked that solved. jajhakallah junaid vi. no need to use ng-change
Yes @Alamgir , its just for checking purpose you can remove it, and no problem, here to help. :)
1

Try this:

<input type="checkbox" ng-model="source.postAdequate">

See here jsfiddle link:

https://jsfiddle.net/avnesh2/5cpm48tc/2/

But it will add source.postAdequate: true/false only if you click, remains objects will remains same.

So if you want to add source.postAdequate: true/false in all add in $scope.reasons before only.

Hope this will help you.

2 Comments

can't use ng-checked and ng-model on same element per docs
Thanks @charlietfl, I forgot to remove, I updated answer, Thanks again.
0

Using ng-model instead of ng-checked will add property internally when checkbox is changed

<input type="checkbox" ng-model="source.postAdequate"></td>

If you must have that property on all objects iterate the array and add it

scope.reasons.forEach(function(item){
   item.postAdequate = false
})

Comments

0

Can you loop the scope object once to initially set postAdequate:false for all items. Then bind that object in checkbox ng-model.

        angular.forEach($scope.reasons, function(e,i){
          e.postAdequate = false;
        });

html code

<table>
<tr ng-repeat="source in reasons">
<td>{{source.sourceName}}</td>
<td>
<input type="checkbox" ng-model="source.postAdequate" ng-click="clickfunction(source.postAdequate)">
</td></tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.