AngularJS ng-checked isn't updating checkbox when model changed in javascript -
i have filtered list of objects being repeated ng-repeat input checkbox beside each item. when list isn't filtered (as when page loaded) don't want checkboxes checked. if list filtered want checkboxes checked (the checkboxes form treeview via css).
the checkbox has ng-checked="{{site.ischecked}}". when list filtered updating ischecked variable on objects within filter javascript code, isn't updating checkbox value. if item filtered out it's removed screen , filtered in again updated ischecked value come through screen.
the html follows:
<ul> <li ng-repeat="site in sites | filtersites:searchbuildings"> <input type="checkbox" ng-checked="{{site.ischecked}}" id="item-f{{site.id}}" /><label for="item-f{{site.id}}">{{site.site}}</label> <ul> <li ng-repeat="building in site.filteredbuildings"> <a href="#" ng-click="getbuilding(building)">{{building.address}}</a> </li> </ul> </li> </ul>
the js is:
var filtered = []; searchbuildings = searchbuildings.tolowercase(); angular.foreach(sites, function (site) { var added = false; var sitematch = false; site.ischecked = true; if (site.site.tolowercase().indexof(searchbuildings) !== -1) sitematch = true; angular.foreach(site.buildings, function (building) { if (sitematch == true || building.search.tolowercase().indexof(searchbuildings) !== -1) { if (added == false) { added = true; filtered.push(site); site.filteredbuildings = []; } site.filteredbuildings.push(building); } }); }); return filtered;
sorry if code isn't pretty - i'm new angularjs , js , still trying work out how things link together.
thanks
you should use ng-model
there provide 2 way binding. checking , unchecking value update value of site.ischecked
<input type="checkbox" ng-model="site.ischecked" id="item-f{{site.id}}"/>
Comments
Post a Comment