There are lots of opinions about how to write good Angular code. One of the best style guides out there is the one created by John Papa available at https://github.com/johnpapa/angularjs-styleguide.
It contains a large list of great tips about naming guidelines, application structure, testing, exception handling, and so on…
One advice that I started to implement recently is the usage of ‘Controller Activation Promises’. It solves the problem where you have your start-up logic scattered around across the controller. It places all start-up logic in a consistent place.
How do you this? Simple, create an activate() function and put all your start-up logic inside:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Avengers(dataservice) { | |
var vm = this; | |
vm.avengers = []; | |
vm.title = 'Avengers'; | |
activate(); | |
//////////// | |
function activate() { | |
return dataservice.getAvengers().then(function(data) { | |
vm.avengers = data; | |
return vm.avengers; | |
}); | |
} | |
} |