angularjs ng-if difference between value and function

前端 未结 2 466
一整个雨季
一整个雨季 2020-12-16 17:32

Is there any difference using ng-if with a value or with a function ?

ng-if=\"myvalue\"
ng-if=\"myfunc()\"

UPDATE (for a better understandi

相关标签:
2条回答
  • 2020-12-16 18:09

    For angular both are expression, that it evaluates in context of current scope. Angular does this on each digest cycle.

    There are more ways to shoot in the foot if you are using the function way. myfunc could do

    $scope.myfunc=function() {
       //do some time consuming work
       return data;
    };
    

    In such a case the binding evaluation on each digest cycle will make your binding and app slow.

    So if you are using function based binding make sure that functions return fast by doing minimum processing.

    0 讨论(0)
  • 2020-12-16 18:31

    Calling functions on ng-repeat directives can cause some performance issues, but if it's a one off evaluation then there isn't really a difference as far as I've noticed.

    I always try to evaluate the property: ng-if="myValue" would evaluate the scope variable $scope.myValue

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