What's the AngularJS “way” of handling a CRUD resource

前端 未结 5 928
半阙折子戏
半阙折子戏 2021-01-30 03:38

I am interested in moving a lot of my client\'s \"logic\" away from Rails routing to AngularJS. I have slight confusion in one topic and that is linking. Now, I do understand th

5条回答
  •  一生所求
    2021-01-30 04:04

    Option 1: $http service

    AngularJS provides the $http service that does exactly what you want: Sending AJAX requests to web services and receiving data from them, using JSON (which is perfectly for talking to REST services).

    To give an example (taken from the AngularJS documentation and slightly adapted):

    $http({ method: 'GET', url: '/foo' }).
      success(function (data, status, headers, config) {
        // ...
      }).
      error(function (data, status, headers, config) {
        // ...
      });
    

    Option 2: $resource service

    Please note that there is also another service in AngularJS, the $resource service which provides access to REST services in a more high-level fashion (example again taken from AngularJS documentation):

    var Users = $resource('/user/:userId', { userId: '@id' });
    var user = Users.get({ userId: 123 }, function () {
      user.abc = true;
      user.$save();
    });
    

    Option 3: Restangular

    Moreover, there are also third-party solutions, such as Restangular. See its documentation on how to use it. Basically, it's way more declarative and abstracts more of the details away from you.

提交回复
热议问题