简体   繁体   English

使用Angular工厂服务

[英]Using Angular Factory Service

I'm trying to follow Code School's Staying Sharp with Angular Soup to Bits. 我正尝试遵循Code School的《尖锐的尖角汤》。
I've come to the point where I've created a note factory service and implement the $resource service. 我已经到了创建注释工厂服务并实现$resource服务的地步。

I $scope.notes = Note.query() and then console.log($scope.notes) but nothing appears in my dev tools console of my browser. $scope.notes = Note.query() ,然后是console.log($scope.notes)但浏览器的开发工具控制台中没有任何显示。 What am I doing wrong? 我究竟做错了什么?

My GitHub repo with the latest commit where this is happening can be found here . 可以在此处找到我的GitHub存储库,其中包含最近进行的提交。

The way $resource has implemented $scope.notes = Note.query() will assign data returned from ajax directly to $scope.notes variable once Note.query() promise gets resolved. $resource实现$scope.notes = Note.query()将在Note.query()承诺得到解决后将从ajax返回的数据直接分配给$scope.notes变量。

But the way you are doing is, you are expecting to get response from async call as soon as you make, for getting response in console you should wait till that promise gets resolved. 但是您正在做的是,您希望尽快从async调用中获得响应,要在控制台中获得响应,您应该等待该诺言得到解决。

Note.query().$promise.then(function(response){
    $scope.notes = response;
    console.log($scope.notes);
});

Update 更新资料

The other problem was OP has mistakenly registered multiple instances of the NotesIndexController controller, because of which many controller are getting registered but last one controller instance got binded to view. 另一个问题是OP错误地注册了NotesIndexController控制器的多个实例,因为要注册许多控制器,但是最后一个控制器实例被绑定到视图。 By correcting all other controller name. 通过更正所有其他控制器名称。 This issue has been fixed. 这个问题已被解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM