Ng-Show called multiple times

前端 未结 1 619
旧巷少年郎
旧巷少年郎 2021-01-24 07:01

I have a button with the ng-disable directive attached and a function to evaluate the button state. When the view loads, I notice that the function is called around 6-8 times. I

相关标签:
1条回答
  • 2021-01-24 07:50

    ng-show will be evaluated on every digest cycle - it doesn't know (at that level of abstraction) what is checked inside ng-show, so must reevaluate anytime the result could have changed. What you may want to do is something like:

     $scope.canDelete = checkIfCanDelete();
     checkIfCanDelete = function() {
         //your check
         $scope.canDelete = Site.selected && Site.selected.children.length;
     }
    
     $scope.$watch('Site.selected', function() {
         checkIfCanDelete();
     }        
    

    But honestly, if it's not computationally intensive (and this isn't), it's probably easier and clearer to just leave it as it.

    0 讨论(0)
提交回复
热议问题