简体   繁体   中英

Angular checkboxes for objects within array

I have an array, that contains objects. Each object has a boolean varaible (array[i].check). I want to make a table using AngluarJS that by clicking on a checkbox it directly change a boolean value of a specific object within my array.I use:

 <table> 
    <tr  data-ng-repeat="a in array">
            <td style="width:200px;"> {{a.name}}</td>   
            <td><input type="checkbox"  ng-model="a.check"  ng-checked="a.check"></td>
    </tr>
 </table>

But the value of every boolean remains false always. What I am doing wrong?

Can you try this method of using ng-model alone, I have created a fiddle with the working method, apply this code, then share back with the issue and I will resolve it for you!

 var app = angular.module('myApp', []); app.controller('MyController', function MyController($scope) { $scope.array = [{id:1, name: "test1", changeThisValue: false},{id:2, name: "test2", changeThisValue: true},{id:3, name: "test3", changeThisValue: true},{id:4, name: "test4", changeThisValue: true},{id:5, name: "test5", changeThisValue: true}]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-controller='MyController' ng-app="myApp"> <table> <tr data-ng-repeat="a in array"> <td style="width:200px;"> {{a.name}}</td> <td> <input type="checkbox" ng-model="a.changeThisValue"> </td> </tr> </table> <pre style="width:100px;white-space: pre-wrap;">{{array}}</pre> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM