Here's a strange behavior I want you to help me to examine: http://jsfiddle.net/m3Z8r/2/
I'm curious about why Angular calls symbolsLength() function more times that I've expected:
template1 to template2 it fires again (but there's no obvious need)If you set initial templateName to "template2", function symbolsLength() will not be called, which is fine.
Please, can someone explain me what's going on here?
P.S. I understand that there's no need in symbolsLength() function since we can just write {{ symbols.length }}, but let's pretend that this function does something very important, for example, contacting service.
1. From the first time it was called twice (was expected to be called only once)
I stepped through the code and saw that this is because each watch function that is created for any binding holds a last value property and in the first phase the last value is different than the expression value which sets the dirty flag to true thus triggering a new pass.
2. When switching from template1 to template2 it fires again (but there's no obvious need)
I doesn't matter that the scope value hasn't changed once a new template is included new watches are created so step 1. is repeated