I've specified a function in my controller like this:
$scope.myFunction = function(){ console.log('test'); }
I want this function to be triggered when a selectbox has been changed. Therefore I apply ng-change on the select element like this:
<select ng-options="..." ng-model="..." ng-change="myFunction();"></select>
But I also want the myFunction function to be triggered when my page is loaded. So I thought to turn my function into a IIFE:
($scope.myFunction = function(){ console.log('test'); }());
However, now the function is only triggered on pageload and not by ng-change. I noticed that when I change the parenthesis, the function also gets triggered by ng-change:
($scope.myFunction = function(){ console.log('test'); })();
Can someone explain why this even matters?
Thanks alot!
Such parens typically indicate that the function expression will be immediately invoked, and the variable will contain the result of the function, not the function itself. This can save someone reading your code the trouble of having to scroll down to the bottom of what might be a very long function expression to see if it has been invoked or not.
– Cephalonia