javascript - Error caused when set checkboxes as checked using angularjs -
this html:
<span ng-repeat="category in categories"> <label class="checkbox-inline" for="{{category.id}}"> <input type="checkbox" ng-checked="check(category.id)" ng-click="saverolemenu($event,category.id)" name="group" id="{{category.id}}" /> {{category.name}} </label> </span>
this controller:
//get menus authorityservice.getmenus().then(function(response) { $scope.categories = response.data.data; //get menus success }) //get user's menus var roleid = sessionstorage.getitem("roleid"); authorityservice.getmenusbyroleid(roleid).then(function(response) { $scope.usermenu = response.data.data; //get user's menu success }) $scope.saverolemenu = function($event, id) { var checkbox = $event.target; if (checkbox.checked) { var roleid = $location.search().id; authorityservice.saverolemenu(roleid, id).then(function(response) { if (response.data.code == 0) { alert("success!"); } }) } } $scope.check = function(value) { //error caused here (var = 0; < $scope.usermenu.length; i++) { if(value == $scope.usermenu[i].id){ return true; }else{ return false; } } }
i want set checkbox checked if value in $scope.usermenu
,but in check function,caused error "cannot read property 'length' of undefined",what's reason error?
cannot read property 'length' of undefined
means $scope.usermenu
not defined. define $scope.usermenu before assigning value it.
$scope.usermenu = []; var roleid = sessionstorage.getitem("roleid");
Comments
Post a Comment