Why angularjs will invoke function `name()` twice?
Solution 1:
In AngularJS, anything wrapped in double curly braces is an expression that gets evaluated at least once during the digest cycle.
AngularJS works by running the digest cycle continuously until nothing has changed. That's how it ensures the view is up-to-date. Since you called a function, it's running it once to get a value and then a second time to see that nothing has changed. On the next digest cycle, it will run at least once again.
It's generally a good idea to only call idempotent methods (like name
) from the template for this very reason.