javascript - Why is my angular script running an infinite loop? -
what trying check 3 boxes via event click upon page load use of loop. happens runs infinite loop page hangs. here code:
<!doctype html> <html > <head> <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script> </head> <body ng-app="ngtoggle"> <div ng-controller="appctrl"> <input type="checkbox" ng-model="dean" ng-click="btnchange($event, values, 1)" id="check-1" name="one" class="here" > <input type="checkbox" ng-model="armada" ng-click="btnchange($event, values, 2)" id="check-2" name="one" class="here" > <input type="checkbox" ng-model="armada" ng-click="btnchange($event, values, 2)" id="check-3" name="one" class="here" > <!--<p ng-repeat="btn in btns">--> <!-- <input type="checkbox" ng-model="btn.bool" class="here" > {{ btn.value }}--> <!--</p>--> {{btn }} {{values }} </div> <script type="text/javascript"> angular.module('ngtoggle', []) .controller('appctrl',['$scope', '$timeout', function($scope, $timeout){ $scope.btns = [{}, {}, {}]; $scope.values = []; $scope.btnchange = function(event, model, val){ _this = angular.element(event.target); x = _this.prop("checked"); if(x){ model.push(val); }else{ index = model.indexof(val); model.splice(index, 1); } }; ids = [1, 2, 3]; $timeout(function() { for(x=0;x<ids.length;x++){ angular.element("#check-"+ids[x]).trigger("click"); } }, 1000); }]); </script> </body> </html>
here plunker: http://plnkr.co/edit/7dpcvkkllkhrc3ywftq0?p=info
ps
if comment or removed angular.element trigger not occur. if can provide code trigger click events on checkboxes via loop. then great
you can angular way setting default values checkboxes:
as use ng-model, in controller, set dean
, armada
true
.
update:
i've nevertheless debugged js. here plunker of function working. i've used y in loop (x defined) , change bit condition of looping.
anyway, recommend angular... :-)
Comments
Post a Comment