如何返回与AngularJS和安培/编辑REST资源; Restangular [英] How to return/edit REST resource with AngularJS & Restangular

查看:189
本文介绍了如何返回与AngularJS和安培/编辑REST资源; Restangular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Restangular 为AngularJS,远离Mongolab得到一个对象的对象。

我敢肯定它做的承诺,但不知道如何使用/实现这个从旧的Java面向对象的经验来了。

(附注,会像口才Javascript中,一些书或资源帮助我理解的'新'的Javascript的风格?)

小的web应用程序是为残疾学生,并输入/编辑的同学,他们更新他们的家长/监护人每周上学,然后输出报告后所花的时间。

这里的code雨后春笋般冒出来的新形式时返回undefined(AngularJS自举模式UI)

我个人认为Restangular&安培;文档是一个很好的补充所以希望这不会劝阻他人 - 这是只是我不知道足够的

在此先感谢

app.js
...

  $ scope.editStudent =功能(ID){
    $ scope.myStudent = Restangular.one(生,身份证);
    的console.log($ scope.myStudent);
}


解决方案

我Restangular的创作者:)。也许我可以帮你有点这个问题。

所以,你需要做的第一件事就是配置Restangular的的baseUrl。对于MongoLab你平时有一个基础URL,它类似于所有的人。

一旦你明白我的工作,你需要检查响应的格式为:

如果您回应被包在另一个对象或信封,您需要在responseExtractor为解包吧。为此,检查<一href=\"https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how-do-i-get-the-data-in-that-case\">https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how-do-i-get-the-data-in-that-case

一旦你明白我的好了,可以开始做的请求。

所有Restangular请求返回的承诺。棱角分明的模板,能够处理并承诺他们能够显示HTML中承诺的结果。所以,如果承诺还没有解决,这说明不了什么,一旦你从服务器的数据,它的模板的显示。

如果你想要做的是编辑你得到的对象,然后做一放,在这种情况下,你不能与承诺工作,因为你需要改变值。

如果是这样的话,你需要分配的承诺到$范围变量的结果。

有关这一点,你可以这样做:

  Restangular.one(生,ID)获得()。然后(功能(serverStudent){
    $ scope.myStudent = serverStudent;
});

这样,一旦服务器返回的学生,你会分配给这个范围的变量。

希望这有助于!否则评论我在这里!

另外,请查阅与MongoLab这个例子也许会帮助你:)

http://plnkr.co/edit/d6yDka?p=$p$pview

Using Restangular for AngularJS, keep getting an object object from Mongolab.

I'm sure it has to do with Promise but not sure how to use/implement this coming from old Java OO experience.

(Side note, would something like Eloquent Javascript, some book or resource help me understand the 'new' Javascript style?)

The small web app is for Disabled Students and is to input/edit the students, update the time they spend after school and then output reports for their parents/caregivers every week.

Here's the code that returns undefined when popping up a new form (AngularJS Boostrap UI modal)

I personally think Restangular & the documentation is a great addition so hope it doesn't dissuade others - this is just me not knowing enough.

Thanks in advance

app.js ...

$scope.editStudent = function(id) {
    $scope.myStudent = Restangular.one("students", id);
    console.log($scope.myStudent);
}

解决方案

I'm the creator of Restangular :). Maybe I can help you a bit with this.

So, first thing you need to do is to configure the baseUrl for Restangular. For MongoLab you usually do have a base url that's similar to all of them.

Once you got that working, you need to check the format of the response:

If your response is wrapped in another object or envelope, you need to "unwrap" it in your responseExtractor. For that, check out https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how-do-i-get-the-data-in-that-case

Once you got that OK, you can start doing requests.

All Restangular requests return a Promise. Angular's templates are able to handle Promises and they're able to show the promise result in the HTML. So, if the promise isn't yet solved, it shows nothing and once you get the data from the server, it's shown in the template.

If what you want to do is to edit the object you get and then do a put, in that case, you cannot work with the promise, as you need to change values.

If that's the case, you need to assign the result of the promise to a $scope variable.

For that, you can do:

Restangular.one("students", id).get().then(function(serverStudent) {
    $scope.myStudent = serverStudent;
});

This way, once the server returns the student, you'll assign this to the scope variable.

Hope this helps! Otherwise comment me here!

Also check out this example with MongoLab maybe it'll help you :)

http://plnkr.co/edit/d6yDka?p=preview

这篇关于如何返回与AngularJS和安培/编辑REST资源; Restangular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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