Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIFE in AngularJS

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!

like image 522
PhillSlevin Avatar asked Sep 03 '14 12:09

PhillSlevin


People also ask

What is IIFE used for?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The name IIFE is promoted by Ben Alman in his blog.

Where IIFE is used in JS?

One typical use case of an IIFE and closure combination is to create a private state for any variable. In the above example, the secret value can only be changed by using the method as the variable is not available globally and is discarded off as soon the IIFE gets executed.

What is the correct code for IIFE?

(function () { //write your js code here }); Now, use () operator to call this anonymous function immediately after completion of its definition. (function () { //write your js code here })(); So, the above is called IIFE.

How do I write IIFE in JavaScript?

Converting Functions to IIFEs Given any regular function definition, wrap the definition within a closed pair of parentheses, this will create your Function Expression. Lastly, add another pair of parentheses and a semicolon to mark the end of the statement, and you have converted your regular Function into an IIFE.


2 Answers

There is a huge difference between this

($scope.myFunction = function(){ console.log('test'); }());

And this

($scope.myFunction = function(){ console.log('test'); })();

Because the first line assigns the result of the function call, and then only stores it, but it's not a function that it stores.

The second works as expected because you call the function after having assigned it to $scope.myFunction

UPDATE

As helpermethod pointed out in comments, the first line is not an IIFE, because you're not calling the function itself, but only the result of it.

like image 159
axelduch Avatar answered Sep 19 '22 14:09

axelduch


Without seeing all of your code it is hard to tell. You are not using an IIFE, you are executing your own function and setting it to the $scope variable. Also, an IIFE will not make it run on page load. Instead of trying to correct all of that, try using code more like the example below.

Try creating a controller in an IIFE and updating your HTML like this:

<div ng-controller="MyCtrl as vm">
     <select ng-options="vm.someOptions" 
        ng-model="vm.someModel" 
        ng-change="vm.myFunction()"></select>
</div>

and your controller

(function(){
    angular.module('myapp').controller('MyCtrl', MyCtrl);

    function MyCtrl() {
        var vm = this;

        vm.someModel; 
        vm.someOptions = []; // set these
        vm.myFunction = myFunction;

        activate();

        function activate() {
            myFunction();
        }

        function myFunction() {
            // TODO: will be called onchange and
            // when controller starts
        }

    }

})();
like image 35
John Papa Avatar answered Sep 20 '22 14:09

John Papa