[英]ngResource/$resource .query() not loading data
I have built an test app very similar to the one from AngularJS' tutorial, with 1 major difference: It is initialized with Yeoman. 我构建了一个与AngularJS教程中的应用程序非常相似的测试应用程序,但有1个主要区别:它是使用Yeoman初始化的。
My factory code looks like this: 我的工厂代码如下所示:
var deClashServices = angular.module('deClashServices', ['ngResource']);
deClashServices.factory('Session',
['$resource',
function ($resource) {
return $resource('views/sessions.json');
}]
);
Yes, angular-resource.js has been added to index.html. 是的,angular-resource.js已添加到index.html。 As well as my Controller, and Service js files.
以及我的Controller和Service js文件。 Yes, deClashServices has been listed as a dependency on the ng-app, as seen here in my app.js:
是的,deClashServices已被列为对ng-app的依赖项,如我的app.js中所示:
var declashAngularApp = angular.module('declashAngularApp', [
'ngRoute',
'deClashServices',
'deClashControllers'
]);
declashAngularApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/sessions', {
templateUrl: 'views/sessions.html',
controller: 'SessionListCtrl'
})
.otherwise({
redirectTo: '/'
});
}]);
and here is my Controller: 这是我的控制器:
var deClashControllers = angular.module('deClashControllers', []);
deClashControllers.controller('MainCtrl',
['$scope', 'Session',
function ($scope, Session) {
$scope.sessions = Session.query();
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
}]
);
in main.html
, which is under MainCtrl
, {{awesomeThings}}
produces the array of 3 strings, as expected. 在
MainCtrl
中的main.html
, {{awesomeThings}}
产生了3个字符串的数组,与预期的一样。 But {{sessions}}
produces an empty array. 但是
{{sessions}}
会产生一个空数组。
To pinpoint that it is .query() that's not loading it, I tried using a factory with a simple JSON file: {"sessions":"number1"}
为了确定不是.query()不会加载它,我尝试使用带有简单JSON文件的工厂:
{"sessions":"number1"}
deClashServices.factory('Session',
['$resource',
function ($resource) {
return $resource('views/simple.json');
}]
);
And my controller is like this: 我的控制器是这样的:
deClashControllers.controller('MainCtrl',
['$scope', 'Session',
function ($scope, Session) {
$scope.sessions = {};
Session.get(function(response) {
$scope.sessions = response.sessions;
});
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
}]
);
This code works. 此代码有效。 Switching back to the JSON array and trying a callback with
.query.$promise.then(callback-function)
however, does not work. 但是,切换回JSON数组并尝试使用
.query.$promise.then(callback-function)
不起作用。
I'm pretty lost and confused now, as the tutorial code from AngularJS with almost the exact same structure works. 现在,我非常迷茫和困惑,因为AngularJS的教程代码几乎具有完全相同的结构。
I suspect that there's something wrong in the way I'm using .query()
, but at the same time the same way is used by the Angular Tutorial and there isn't a problem there. 我怀疑我使用
.query()
的方式有问题,但是同时Angular Tutorial使用了相同的方式,并且那里没有问题。
Just in case that it might be outside the few code snippets I've shown, here in the code for the entire project: github 以防万一它可能超出了我显示的一些代码片段,这里是整个项目的代码: github
Angular's resource-service doesnt return promise. Angular的资源服务不会返回承诺。 It returns an empty object so that when the request returns from server resource-service can populate that object with fetched data because it has a reference to that object.
它返回一个空对象,以便当请求从服务器返回时,资源服务可以使用获取的数据填充该对象,因为它具有对该对象的引用。
So if you want to react to returning data you have to use a callback function or better yet use $http-service, because it return promise. 因此,如果您想对返回的数据做出反应,则必须使用回调函数,或者最好使用$ http-service,因为它会返回promise。
I also recommend you take a look at Restangular 我还建议您看看Restangular
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.