Angularjs决心与控制器串 [英] Angularjs resolve with controller as string
问题描述
我的写作角度控制器的风格是这样的(使用控制器名称,而不是功能)
My style of writing angular controllers is like this (using controller name instead of function)
angular.module('mymodule', [
])
.controller('myController', [
'$scope',
function($scope) {
// Some code here
}
]);
我现在提供i当路由都需要我要定义解决部分:
What I need now is when providing i routes I want to define resolve part:
$routeProvider.when('/someroute', {
templateUrl: 'partials/someroute.html',
resolve: myController.resolve}) // THIS IS THE CRITICAL LINE
由于控制器被定义为一个名称如何完成解决部分波纹管?
Since controller is defined as a name how to accomplish resolve part bellow?
要澄清的更多详细我想路由解决之前从服务器加载一些数据,然后在控制器中使用这些数据。
To clarify more in details I want to load some data from server before route is resolved and then use these data in controller.
更新::要更precise我想每个模块都有根将与之前执行的控制器被称为它的决心功能。解决方案在this帖子(由MISKO Hevery回答)不正是我想要什么,但我没有控制器,功能,但是作为一个名字。
UPDATE: To be more precise I want each module has its "resolve" function that will be called before root with that controller is executed. Solution in this post (answered by Misko Hevery) does exactly what I want but I don't have controllers as functions but as a names.
推荐答案
控制器定义和解决部分是要在路由定义另行规定。
The controller definition and resolve parts are to be specified separately on the route definition.
如果您在模块级定义的控制器需要引用它们作为字符串,因此:
If you define controllers on a module level you need to reference them as string, so:
$routeProvider.when('/someroute', {
templateUrl: 'partials/someroute.html',
controller: 'myController',
resolve: {
myVar: function(){
//code to be executed before route change goes here
};
});
以上code也显示了如何定义一组将路由变化之前解决的变量。当解决了这些变量可以被注入到一个控制器,以便采取从片段上面的例子中,你会写你的控制器像这样:
The above code also shows how to define a set of variables that will be resolved before route changes. When resolved those variables can be injected to a controller so taking the example from the snippet above you would write your controller like so:
.controller('myController', ['$scope', 'myVar', function($scope, myVar) {
// myVar is already resolved and injected here
}
]);
本视频可能会有所帮助: http://www.youtube.com/watch?v=P6KITGRQujQ
This video might help as well: http://www.youtube.com/watch?v=P6KITGRQujQ
这篇关于Angularjs决心与控制器串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!