angularjs - Can a router resolve be set as a function of a controller class? -
background
i've been reading todd motto's opinionated style guide, in particular part relating router resolves - scroll down 'controller.resolve property:' part:
https://github.com/toddmotto/angularjs-styleguide#routing-resolves
i've disliked fact had resolve logic defined in router definition, in theory quite liked solution, in practice, don't see how it's possible?
todd states:
'this keeps resolve dependencies inside same file controller , router free logic'
so understanding of - using example - you'd have controller logic in 1 file (for arguments sake controllers.js) defined as,
app.module('myapp') .controller('mainctrl', mainctrl); function mainctrl() { ///some controller logic here } mainctrl.resolve = { somestuff: function() { //resolve here } }
and in file (routes.js) you'd have,
app.module('myapp') .config(config); function config ($routeprovider) { $routeprovider .when('/', { templateurl: 'views/main.html', controlleras: 'vm', controller: 'mainctrl' resolve: mainctrl.resolve }); }
to prevent polluting global scope, i'm presuming both of these files logic within iife. place mainctrl
exist injectable value within angular app. , can't inject controller config
, there's no way know mainctrl
is, let alone access resolve
method.
question
or missing in way should implemented?
check out "iife scoping" piece:
to avoid polluting global scope our function declarations passed angular, ensure build tasks wrap concatenated files inside iife"
we @ compile time, nothing global. wrap entire module inside iife, , that's :)
i wouldn't recommend wrapping every file inside iife, because that's needless function calls @ runtime, if had 1000 files instance. 1 iife per module approach :)
Comments
Post a Comment