我需要将一些内容数据提供给我的控制器:
state('admin.businesses.employees.all', {
resolve: {
executorsListTitle: 'All employees',
executorsEmptyListMessage: 'Add the first employee'
},
url: '/all',
controller: 'ExecutorsController',
templateUrl: 'templates/admin/executors/index.html'
})
Run Code Online (Sandbox Code Playgroud)
和一个控制器代码:
module.controller(
'ExecutorsController',
[
'$scope', '$rootScope', '$state',
'$stateParams', '$modal', 'executorsListTitle',
'executorsEmptyListMessage', 'Executor',
function($scope, $rootScope, $state, $stateParams, $modal, executorsListTitle, executorsEmptyListMessage, Executor) {
// Some code
}
)
Run Code Online (Sandbox Code Playgroud)
但是当我试图进入这种状态时,我无法做到这一点 - 按下按钮就什么都不做; 如果我从状态描述中删除了解决方案,那就很好.我做错了什么?谢谢!
国家机器的决心期望钥匙和工厂.该文件指出:
- key - {string}:要注入控制器的依赖项的名称.
- 工厂 - {string | function}
当你向工厂提供一个字符串时:
如果是string,那么它是服务的别名.
如果要返回字符串,可以执行以下操作:
state('admin.businesses.employees.all', {
resolve: {
executorsListTitle: function() {
return 'All employees';
},
executorsEmptyListMessage: function() {
return 'Add the first employee';
},
},
url: '/all',
controller: 'ExecutorsController',
templateUrl: 'templates/admin/executors/index.html'
})
Run Code Online (Sandbox Code Playgroud)
如果您使用的是静态数据(要解析的字符串),您还可以使用自定义数据属性:
state('admin.businesses.employees.all', {
data: {
executorsListTitle: 'All employees',
executorsEmptyListMessage: 'Add the first employee'
},
url: '/all',
controller: 'ExecutorsController',
templateUrl: 'templates/admin/executors/index.html'
})
Run Code Online (Sandbox Code Playgroud)
如果使用此方法,则可以在控制器中访问如下数据:
$state.current.data.executorsListTitle
Run Code Online (Sandbox Code Playgroud)
您可以直接使用此方法的字符串.这是自定义数据属性的文档.