Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

IIFE Syntax for Angular Controllers

2/1/2016

0 Comments

 
If you want to be a jedi level coder, you have to be careful to not pollute the global namespace with your methods and properties of your controllers (components in angular 2). It's pretty straightforward to do this, but it's sort of a tricky syntax. Suppose your controller now is ...code..., and this could be how you wrap it in an IIFE:

(function(){
    ...code... 
})()

IIFE's Actually Do Make Sense

Really quickly I'll try to break this down so that it makes a little sense, but to do it I'm going to go backwards. Look at the final opening and closing parens. They will make the function execute immediately, hence the words immediately invoked in the name. The closing parent to the left of it wraps everything and corresponds to the very first opening paren in the code. The curly brackets contain all of your executing code (the controller declaration and logic). Then finally, the opening and closing parens to the right of the function keyword just signify that this anonymous function takes no parameters. 

Why to Use It

As I mentioned earlier, using IIFE's allows you to use less global variables and not pollute the global namespace. for more info on this see a style guide link johnpapa's here: ​https://github.com/johnpapa/angular-styleguide#iife

A Great Example

I wish I could take credit for this example, but I actually took it from a stack overflow answer from john papa (yes, the same author of the angular style guide!). Original link to that stack question is here: http://stackoverflow.com/questions/25644973/iife-in-angularjs. 

snippet of html:​
<div ng-controller="MyCtrl as vm">
     <select ng-options="vm.someOptions" 
        ng-model="vm.someModel" 
        ng-change="vm.myFunction()"></select>
</div>
javascript 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
        }

    }

})();
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletter called,
    "The Triple Gainers"?

    Sign up here!

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021