我有像这样的app.js代码
var MYApp = angular.module('myApp', ['ngRoute', 'myAppServices', 'ngSanitize'])
.config(myRouter);
angular.module('myAppServices', ['ngResource'])
.factory('GridsAPI', function($resource) {
return {
Users: $resource('/MY/system/users/grid'),
Groups: $resource('/MY/system/groups/grid'),
GroupList: $resource('/MY/system/getGroupList')
};
});
MYApp.controller('CreateUserController', ['$scope', 'groupList', function($scope, groupList) {
$scope.test = 'Hello';
$scope.groups = groupList;
debugger; //here I am getting correct values of test and groups
}]);
function myRouter($routeProvider) {
$routeProvider
.when('/users/create', {
templateUrl: '/MY/system/users/create',
controller: 'CreateUserController',
resolve: {
groupList: function(GridsAPI) {
return GridsAPI.GroupList.get().$promise;
}
}
});
}
这是我的HTML
<div class="white-box" ng-controller="CreateUserController">
<h1>{{test}}</h1>
<pre>{{ groups | json }}
</div>
在浏览器中我得到了这个,没有任何约束力,我缺少什么?
{{test}}
{{ groups | json }}
在调试器中我正确获取对象...请参阅屏幕截图
答案 0 :(得分:0)
您的决心会返回一个承诺。此承诺将在您到达时立即包含您的群组。
MYApp.controller('CreateUserController', ['$scope', 'groupListPromise', function($scope, groupList) {
$scope.test = 'Hello';
$scope.groups = [];
groupListPromise.then(function(groupList){
$scope.groups = groupList;
});
debugger; //here I am getting correct values of test and groups
}]);
function myRouter($routeProvider) {
$routeProvider
.when('/users/create', {
templateUrl: '/MY/system/users/create',
controller: 'CreateUserController',
resolve: {
groupListPromise: function(GridsAPI) {
return GridsAPI.GroupList.get().$promise;
}
}
});
}