解耦服务
There're some times where you want to use Restangular but you don't want to expose Restangular object anywhere. For those cases, you can actually use the service
feature of Restangular.
Let's see how it works:
// Declare factory
module.factory('Users', function(Restangular) {
return Restangular.service('users');
});
// In your controller you inject Users
Users.get(2) // GET to /users/2
Users.post({data}) // POST to /users
// GET to /users
Users.getList().then(function(users) {
var user = users[0]; // user === {id: 1, name: "Tonto"}
user.name = "Gonto";
// PUT to /users/1
user.put();
})
We can also use Nested RESTful resources with this:
var Cars = Restangular.service('cars', Restangular.one('users', 1));
Cars.getList() // GET to /users/1/cars