从国家的决心和控制器调用Restangular差异 [英] Difference of calling Restangular from state resolve and from controller

查看:201
本文介绍了从国家的决心和控制器调用Restangular差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Restangular和angularJS。

I am using Restangular and angularJS.

下面是我的控制器code。
我无法在控制台上打印输出的老师,我不能与 vm.teacherList 玩。

Below is my controller code. I am not able to print teacher output on console and I am not able to play with vm.teacherList.

案例1:

    angular.module("subjectManagement").controller('subjectTeacherAllocationCtrl',
         function ($scope, $state, $location, Restangular) {
             var vm = this;
             vm.teacherList = Restangular.all("teacher").getList().$object;
             console.log( " vm.teacherList ---> " + vm.teacherList);
     });

 console output is only  `vm.teacherList --->`

当我检查网络XHR,我能够用数据回应老师像

when I am checking Network XHR, I am able to teacher response with data like

[{teacherId: "3", empId: "9", teacherDateOfJoining: "2015-11-02", teacherJoiningGrade: "TGT",…},…]
0: {teacherId: "3", empId: "9", teacherDateOfJoining: "2015-11-02", teacherJoiningGrade: "TGT",…}
1: {teacherId: "4", empId: "10", teacherDateOfJoining: "2015-11-02", teacherJoiningGrade: "TGT",…}

案例2:
 如果我把同样的code线状态的决心,而不是控制。我可以在控制台打印 vm.teacherList ,我可以用 vm.teacherList

.state('subjectTeacherAllocation', {
                        url: '/subject/subjectTeacherAllocation',
                        templateUrl: 'app/subject/edit/subjectTeacherAllocation.html',
                        controller: 'subjectTeacherAllocationCtrl as vm',
                        resolve: {

                            teacher: function (Restangular, $stateParams) {
                                return Restangular.all('teacher').getList();
                            }
                        }
                    })

  angular.module("subjectManagement").controller('subjectTeacherAllocationCtrl',
     function ($scope, $state, $location, teacher, Restangular) {
         var vm = this;
         $scope.teacher = {}
         vm.teacherList = teacher.plain();

         console.log( " vm.teacherList ---> " + vm.teacherList);

 });




console output is only  vm.teacherList --->  vm.teacherList ---> [object Object],[object Object],[object Object],[object Object],[object Object]

我想了解的是什么区别这两个(案例1安培;情况2)
 我如何能实现,打印vm.teacherList情况1,相同方式的情况下2

推荐答案

案例1

根据该文件,的GetList。$对象返回引用到一个空的对象,当请求完成后数据将填充。所以,如果你尝试打印数据马上你就只能看到空对象。当您检查在开发者控制台的网络对象您所看到的完成的请求这就是为什么你看到的数据。

According to the documentation, getList.$object returns the reference to an empty object that the data will populate when the request is done. So if you try and print the data right away you will only see that empty object. When you check the network object in the developer console you are seeing the completed request which is why you see the data.

案例2

既然你是在决心块,状态不会改变,直到所有的请求都完成了。所以从Restangular您的通话必须在下一步之前完成。当你的控制器负载,老师是个完全填充的对象,您可以使用数据符合市场预期。

Since you are in the resolve block, the state does not change until all requests are finished. So your call from Restangular must finish before the next step. When your controller loads, teacher is a fully populated object and you can use the data as expected.

制作案例1的工作像案例2

使用()由Restangular.all返回的承诺。的GetList和成功填充数据。

Use the promise returned by Restangular.all().getList and on success populate the data.

 vm.teacherList = {};
 Restangular.all('teacher').getList().then(function success(data) {
   vm.teacherList = data;
   console.log(vm.teacherList);
 });

这篇关于从国家的决心和控制器调用Restangular差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆