JavaScript AngularJS loops not updating the view angularjs loop not updating

Performing For..Loops or While..Loops does not update the $scope variable. I'm trying to show the progress as loops are incremented.

I have read (http://jimhoskins.com/2012/12/17/angularjs-and-apply.html) that we need to use $apply to force updates if changes are not detected via an event or a promise such ng-click() event or $http call.

I have tried forcing the update using $apply but this does not seem to change anything.

I have tried using $timeout but again the tests were negative.

In the code below I have tried three different tests. Initially I tried a For..loop, then I tried a while..loop and then a while..loop with a $apply. These tests are independent of one another and I have included all three just to show you what variations I've tried.

var myApp = angular.module('myApp', []);

myApp.controller('CounterController', ['$scope', '$timeout', function($scope, $timeout) {


  $scope.counterMax = 5000;


  $scope.startCounter = function() {
    $scope.progress = 0;
    $scope.progress2 = 0;
    $scope.progress3 = 0;
    // for loop $scope test
    for (i = 0; i <= $scope.counterMax; i++) {

      $timeout(function() {
        $scope.progress = i;
      }, 500);
    }

    // while loop $scope test
    var x = 0;
    try {
      while (x < $scope.counterMax) {
        $timeout(function() {
          $scope.progress2 = x;
        }, 2);
        x++;

      }
    } catch (error) {
      console.log("Error: " + error);
    }



    // while loop $scope test with $apply
    x = 0;
    try {
      while (x < $scope.counterMax) {
        $timeout(function() {
          $scope.$apply(function() {
            $scope.progress3 = x;
          });
        }, 2000);
        x++;

      }
    } catch (error) {
      console.log("Error: " + error);
    }
  }

  $scope.startCounter();
}]);

The view

<div ng-controller="CounterController">
      <p>
        Depending on the value for the Counter Max, a loop will be made from zero to 
        the Counter Max value. 
        <br />
        The value in the Progress Indicators should increment gradually until the Counter Max value is reached.
        <br />
        Currently this is not happening. The Progress Indicators are only updated at the 
        end of the loop.
      </p>
      <p>
        Counter Max: <input type="text" ng-model="counterMax" ng-keyup="startCounter()"/>
      </p>
      <p>(for loop $scope test)<br />
      Progress Indicator 1: {{progress}}</p>

      <p>(while loop $scope test)<br />
      Progress Indicator 2: {{progress2}}</p>

      <p>(while loop $scope test with $apply)<br />
      Progress Indicator 3: {{progress3}}</p>
    </div>

You can see the tests I've made here on plnkr: http://plnkr.co/edit/Nl3GMy0DJNJ53PFObAq1?p=preview

Answer:1

I'm studying rxjs Observables and I couldn't understand why the setInterval inside Observable.create is still running even I have unsubscribe to that observable object. Observable is async right? When ...

I'm studying rxjs Observables and I couldn't understand why the setInterval inside Observable.create is still running even I have unsubscribe to that observable object. Observable is async right? When ...

I have a simple set up to find top scores. The goal is to order who received the highest scores and then give the top 3 bonus points. Question: In part two below I am looping through the people and ...

I have a simple set up to find top scores. The goal is to order who received the highest scores and then give the top 3 bonus points. Question: In part two below I am looping through the people and ...

I have basically this structure for my data (this.terms): { name: 'First Category', posts: [ { name: 'Jim James', tags: [ 'nice', 'friendly' ...

I have basically this structure for my data (this.terms): { name: 'First Category', posts: [ { name: 'Jim James', tags: [ 'nice', 'friendly' ...

During development of a vue project, and got some doubt regarding template / render / VNode. After reading the document https://vuejs.org/v2/guide/syntax.html, and google search, still didn't ...

During development of a vue project, and got some doubt regarding template / render / VNode. After reading the document https://vuejs.org/v2/guide/syntax.html, and google search, still didn't ...

  1. what is a relationship template