angularjs checkbox ng-checked not working -
i have following code :-
<!doctype html> <html lang="en"> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script> </head> <body ng-app="app" ng-controller="ctrl" ng-init="init()"> <div class="container" style="width:400px"> <div class="panel panel-default"> <div class="panel-body"> <form> <div class="form-group"> <label for="selectedbasket">select basket :</label> <select id="selectedbasket" class="form-control" ng-model="selectedbasket" ng-options="b.name b in baskets"> </select> </div> <div ng-repeat="f in fruits" class="checkbox"> <label> <input type="checkbox" value="" ng-checked="selectedbasket !== null && selectedbasket.items.indexof(f) !== -1"> {{ f }} </label> </div> </form> </div> </div> </div> <script> var app = angular.module('app', []); app.controller('ctrl', function($scope) { $scope.init = function() { $scope.baskets = [{'name': 'mary', 'items': ['apple', 'orange']}, {'name': 'jane', 'items': ['banana']}]; $scope.fruits = ['apple', 'banana', 'cherry', 'orange', 'watermelon']; $scope.selectedbasket = null; }; }); </script> </body> </html>
if select mary or jane, can correctly see correct items in basket checked. if manually check fruits , @ mary or jane, doesn't exclude items not in baskets. why ng-checked failing?
bonus question, best practise set selectedbasket null , checking null in directive assuming want nothing default value, there better way?
you've got no ng-model in checkbox manual action isn't registered anywhere.
ng-checked used make 'slave' checkbox can take no manual action. guess should use ng-model initialized ng-check value instead of using ng-checked.
if want keep ng-checked can :
<input type="checkbox" ng-click="selectedbasket.items.push(f)" ng-checked="selectedbasket !== null && selectedbasket.items.indexof(f) !== -1">
in fact it's still wrong... must tired, use toogle function in ng-click add or remove item should better...
Comments
Post a Comment