0

HI have directive with template . in the template I use with long ng-if like:

ng-if="exp1 && (exp== '' || exp == null) && ..."

how can I add the ng-if into a function that return true or false?

it should be updated if the scope has changed. when i tryin with function it cals lot of times

template:

<div ... ng-if="fun()"></div>

my directive:

return {
    restrict: "AE",
    templateUrl: "tmp.html",
    replace: true,
    scope: {
        param: '=',
    },
    link: function(scope, element, attrs, ctrl) {
        scope.fun = function() {
            console.log("log") - show lot of times - does not stop
        }
    }
}
Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
Scopi
  • 663
  • 1
  • 6
  • 21
  • 2
    It's normal that it's called a lot of time. It's called at each change detection, potentially multiple times, precisely because the values it uses to compute the result might have changed. – JB Nizet Mar 07 '17 at 07:55
  • but the value did not changed more than once but other scope params has changed in each second – Scopi Mar 07 '17 at 08:04
  • 1
    There is no way for angular to know which variables you function uses or not, so it needs to call your function to know if it still returns the same thing as before or not, and thus remove or insert the dom element. That is perfectly normal. Nothing to worry about. Just make sure your function is fast, and doesn't have side effects. – JB Nizet Mar 07 '17 at 08:06
  • http://stackoverflow.com/questions/20325480/angularjs-whats-the-best-practice-to-add-ngif-to-a-directive-programmatically – Abdullah Al Noman Mar 07 '17 at 08:08

0 Answers0