How to return/edit REST resource with AngularJS & Restangular

ぃ、小莉子 提交于 2019-12-05 20:16:46

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!