ui-router's 预加载 resolve
obtains data, how to inject it into the controller?
Remarks:
In $state
, I import the controller file via controllerUrl
.
My ui-router version is 0.4.2
.
Routing code ↓
$stateProvider.state('dashboard', {
url: '/dashboard',
templateUrl: 'views/dashboard/index.html',
resolve: {
getList: function ($stateParams, $NetRequest){
var options = {type: 'charts'};
$NetRequest.post(options).then(function($ref){
return $ref.data;
});
}
},
controllerUrl: 'views/dashboard/index',
controller: 'DashboardController',
controllerAs: 'vm'
});
Service Code ↓
service.post = function($options){
var deferred = $q.defer();
// 省略请求函数...
return deferred.promise;
};
If you inject getList
directly into the controller at this time, the result will be undefined
.
I read the official documentation and there is a $inject
parameter, but unfortunately there is no example. I don’t know how to use it.
The problem is solved, the method is actually very simple - -
'//Get data here'. . . . Did you return your promise?