javascript - Update view from while loop with in angular controller -


i in process of learning angular.js , know if there way update view while loop. have button clicked calls function while loop increments variable each iteration. there button cancel loop. have read , understand $apply/$digest loop angular uses update view. have tinkered $interval , had working, in end not fast enough want this.

so there way variable incremented in while loop printed screen. here code have far.

learningangular.js

    var myapp = angular.module("myapp", []);      myapp.controller("mycontroller", mycontroller);            function mycontroller($scope, $interval) {          var count = true;          $scope.counter = 0;                $scope.start_count = function(){              //promise = $interval(function() {              //   $scope.counter++;              //},1);              while (count == true) {                  $scope.counter++;              }             }                  $scope.stop_counting = function(){              //$interval.cancel(promise);              count = false;          }      }
    <!doctype html>            <html>      <head>          <title>learning angular</title>          <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>      </head>            <body ng-app="myapp" ng-controller="mycontroller">                    <button ng-click="start_count()">start counting</button><br>          counter: <span>{{counter}}</span><br>                    <button ng-click="stop_counting()">stop counting</button>            </body>      </html>

edit happens when ng-click calls start_count() function code goes while loop. if have debugger open in chrome , click button put break point @ $scope.counter++ can see counter being incremented. isn't displaying on screen. don't want use setinterval or $interval functions provided aren't fast enough intend use code for. in nut shell comparing simulated power ball numbers chosen user 1 computer draws. setinterval , $interval functions take long where. computer can check couple million in few seconds not if restricted interval.

while loop you've implemented happens within single iteration of $apply/$digest, never see changes of counter. i'm not sure right way achieve expect behavior angular, can implement pure javascript using setinterval or settimeout functions. logic same you've implemented except you'll call setinterval instead of starting while loop. hope helps.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -