Skip to content Skip to sidebar Skip to footer

Function Called Multiple Times From Template

In my template I have something like this: {{formatMyDate(date)}} but date is a scope variable that is not immediately available so this expression will call the function formatMy

Solution 1:

I would recommend you to do things differently:

Either use the date $filter or if you are doing something VERY unique and the date $filter is not good enough for you, then you could create your own $filter, like this:

app.filter('formatMyDate', function () {
    returnfunction (date) {
        if (!date) return"";
        var result;
        //your code here    return result;
    };
});

And use it like this in your template:

{{date | formatMyDate}}

UPDATE:

I guess that I didn't quite answer your question, I just gave you advice on how to improve your code. This time I will try to answer your question:

The $digest cycle is the stage in which Angular ensures the changes of the model have settled, so that it can render the view with the updated changes. In order to do that, Angular starts a loop in which each iteration evaluates all the template expressions of the view, as well as the $watcher functions of the $scope. If in the current iteration the result is the same as the previous one, then Angular will exit the loop. Otherwise, it will try again. If after 10 attempts things haven't settled, Angular will exit with an error: The "Infite $digest Loop Error" (infdig).

That's why the first time that the $digest cycle runs all the expressions are evaluated (at least) twice. And then Every time that you make a change to the $scope or that one of the $watchers of the $scope gets triggered, the $digest cycle will run again in order to make sure that things have settled, so your expressions will be evaluated again. This is how Angular makes "data-binding" happen, it's a normal behaviour.

So in your case, when in your template you do this: {{formatMyDate(date)}} or this {{date | formatMyDate}} you're defining Angular expressions that will be evaluated every time that the $digest cycle runs, which as you can imagine is very often. That's why is very important to make sure that the $filters (or functions) that you use in your view are efficient and stateless.

Solution 2:

You can do this:

{{date && formatMyDate(date)}}

will only execute the second case if the first condition exists and is different from null and undefined.

Check this fiddle: http://jsfiddle.net/HB7LU/7512/

Post a Comment for "Function Called Multiple Times From Template"