Mario MG
Mario MG

Reputation: 419

Refresh $scope while in $apply

In my HTML, I have an element with a ng-click attribute to trigger a function. This function contains a loop which calls another function in each step. Now, I would like to refresh my $scope after every step of the loop, but:

Here's my code:

// IN MY HTML
<th class="groupHeader" ng-repeat="j in groupsMain" ng-click="thClickColumn(j)">{{j}}</th>

// IN MY CONTROLLER
$scope.thClickColumn = function(j) {
    // some code
    for ( var i = 1 ; i <= 7 ; i++ ) {
        _this.tdClick(i,j) ; // I'd like my $scope to refresh after doing this
        if ( _this.inputElement === false ) { return ; } // Also, this bit here should break the loop if the condition is met (_this.inputElement is returned by the function above)
    }
} ;

Thanks!

Upvotes: 3

Views: 189

Answers (1)

MarkoCen
MarkoCen

Reputation: 2324

actually you could use $timeout, just make sure the promise chain is correct:

$scope.thClickColumn = function(j){

     var iterator = 7;

     function call(){
         if(iterator > 0){
             _this.tdClick(iterator, j);
             $timeout(function(){
                 iterator--;
                 if(_this.inputElement !== false){
                     call()
                 }
             })
         }
     }

     call();
}

Upvotes: 1

Related Questions