Pattern to initialize a function when angular component launch

369
July 05, 2017, at 03:07 AM

Recently I had initialize a function when the angular component launch.

I had seen this comment and I'd like to know if is it a good pattern or could be better declare the function and call it after such the code below:

angular.module('app').controller('control',[...,
  function(){
    ...
    var init = function () {
      //what i wish run on launch
    };
    // function call to launch
    init();
  }
)

or as mentioned in comment:

(function(){
    //code to run
}());

What do you have been used or even are there known problems when use this last approach?

Answer 1

I would suggest you use principal of "bindable members on top" and use function declarations instead of function expressions so that functions can be hidden at bottom of controller and will get hoisted.

angular.module('app').controller('control',[...,
  function(){

    // function call to launch
    init();
    // declare functions at bottom
    function init() {
      //what i wish run on launch
    }
  }
)

Reference: John Papa Angular Style guide

Answer 2

Till angular 1.4.x I like and also john Papa style guides preferred to use 1st approach to call init function at the end by ensuring everything got initalize before.

You can't consider a second option here. It's an IIFE, its more likely to define your function in isolated scope instead of declaring them in global.

Apart from that these days if you're specifically talking about component then you should use $onInit angular component lifecycle hook to call initialisation code.

angular.module('app').controller('control',[...,
  function(){
    var vm = this;
    vm.$onInit = $onInit;

    function $onInit() {
      //what i wish run on launch
    };
  }
)
.component('myComponent', {
  controller: 'control',
  templateUrl: 'sometemplate.html'
})
Rent Charter Buses Company
READ ALSO
Having troubles pushing into an array which is a property of an object

Having troubles pushing into an array which is a property of an object

I have this array called "players" which stores all the players, each element of this array contains each individual player stored as an object, and this player object contains a property called "time" which in it of itself is an arrayWhen ever I try to push an integer...

233
Is there a way to access FM / AM radio from JavaScript? [on hold]

Is there a way to access FM / AM radio from JavaScript? [on hold]

I built a webapp to listen to radioAnalysing it's usage it came to light that about 90% of the users listen to stations they could receive via traditional radio broadcasting

264
how to convert x-www-form-urlencoded payload to json

how to convert x-www-form-urlencoded payload to json

i am using the npm request module to post databy default the posy is of x-www-form-urlencoded so on the server side i am receiving the data like this

570
multiple promises running in parallel, $q.all needs chaining?

multiple promises running in parallel, $q.all needs chaining?

I have 3 parallel promises or api requests once all teh three are done, I need to call another api request based on the second promise and then finally callthen( of $q

270