Skip to content Skip to sidebar Skip to footer

How To Assign Variables To An Aliased Controller Inside A Function?

(Disclaimer: I don't really know if this question fits the Stackoverflow definition of 'question', since I already have (more than) one solution for the problem, I just don't like

Solution 1:

You can bind the function's this to the controller:

langService.load({
  callback: languagesLoaded.bind(this)
});

For IE < 9 a polyfill would be needed because bind is available as of ECMAScript 5.

Solution 2:

Your approach #1 seems the best. You should be careful when using this since it changes its meaning based on its context, for example, like in a function. Sometimes you wouldn't even know this, if you are using a 3rd party library.

I find this guide useful. From the guide:

functionCustomer() {
    var vm = this;
    vm.name = {};
    vm.sendMessage = function() { };
}

Post a Comment for "How To Assign Variables To An Aliased Controller Inside A Function?"