Hari Das
Hari Das

Reputation: 10849

Angular $digest cycle refreshes the entire view instead of a portion

I am facing a strange issue in Angular 1 which is causing the script to go into infinite loop and eventually the browser hangs.

Here is what I am trying to do

<script>
    $scope.A = true;
    $scope.B = [{blah},{blah}];
    $scope.updateB = function(){
        $scope.B.push({blah});
    }
    $scope.D = function(key){
        $scope.A = false;
        return key.name;
    }
</script>

    <div ng-if="A">
        <button ng-click="updateB()"></button>
    </div>

    <div ng-repeat="key in B">
      {{D(key)}}
    </div>

So basically I want to hide first div after the button is pressed. I know I can do it in "updateB" function. But no, I want to do that in "D", after the expression is evaluated. It goes into an infinite loop.

Can anybody suggest me what is going wrong here ?

Upvotes: 0

Views: 35

Answers (1)

L. Figueredo
L. Figueredo

Reputation: 278

This happens because your function D() will be executed on every angular digest cycle. Even if you have only one element in B. If you have no elements on B, your function won't be executed.

This may help you to understand how digest cycle works: The Digest Loop and $apply and Understanding the Digest Cycle

Upvotes: 1

Related Questions