在angularjs $资源不能正常工作 [英] $resource in angularjs does not work properly

查看:122
本文介绍了在angularjs $资源不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用角Django的我已经实现了使用Django tastypie一个REST API,
这里的code的角资源:

i'm trying to use angular with django i've implemented a REST api with django tastypie, here's the code for the angular resource:

angular.module('CourseServices', ['ngResource']).
    factory('Course', function($resource){
  return $resource('api/v1/course/:courseId/?format=json', {}, {
    query: {method:'GET', params:{courseId:'1'}, isArray:true}
  });
});

和这里就是我想要得到它:

and here is where i'm trying to get it:

var course = Course.get({courseId:$routeParams.courseId});
console.log(course);

这code这个记录在浏览器中:

this code logs this in the browser:

Resource
course_pic: "http://localhost:8000/media/course_pics/2012/10/24/Apple-Launches-Genius-Recommendations-for-Apple-TV-2.png"
creation_time: "2012-10-24T06:28:11+00:00"
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed faucibus cursus mi, in laoreet dolor placerat vitae. ."
id: "1"
instructor: "/api/v1/user/1/"
name: "Computation Theory"
resource_uri: "/api/v1/course/1/"
subscribers: 0
update_time: "2012-10-24T06:28:11+00:00"
__proto__: Resource

所以我有我想要的数据,但是当我尝试在我的控制器访问例如course.id我得到了一个未定义!我不知道为什么! REST的服务器响应是JSON。

so i have the data i want but when i try to access for instance "course.id" in my controller i get undefined! i have no idea why! the REST server response is json.

推荐答案

要解决你的,我们需要了解 $资源的内部运作第一的问题。还有一个问题,一个较长的答案这里,但总之,我们需要认识到, $资源做的异步的要求,即使其语法表明,我们正在处理同步的。

To solve the problem you are having we need to understand the inner workings on $resource first. There is a question and a longer answer here, but in short we need to realize that $resource does asynchronous calls even if its syntax suggest that we are dealing with synchronous ones.

什么你的情况正在发生的事情是,当您试图访问你的控制器查询数据这些数据还没有准备好。一旦到$资源AngularJS通话将只返回一个空的对象(一种占位符),并会在填充数据(属性),只有当响应从服务器回来。

What is happening in your case is that when you are trying to access query data in your controller those data are not ready yet. Upon a call to the $resource AngularJS will just return an empty object (kind of placeholder) and will fill in data (properties) only when response comes back from the server.

实际上你面对这里的是一个时机的问题。要解决此调用查询时,您可以使用一个回调方法,类似的规定:

Essentially what you are facing here is a timing issue. To work around this you can use a callback when invoking the query method, something along those lines:

var course; Course.get({courseId:$routeParams.courseId}, function(data){
    console.log(data);
    course = data;
    //course.id should be defined here 
});

因此​​,再次: $资源方法仍然是异步的,即使语法可能表明并非如此。你需要,如果你想确保数据已经准备好应对的成功回调的数据。

So, once again: $resource methods are still asynchronous even if the syntax might suggest otherwise. You need to deal with the data in the success callback if you want to be sure that data are ready.

这篇关于在angularjs $资源不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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