Angular JS: Full example of GET/POST/DELETE/PUT client for a REST/CRUD backend?

后端 未结 3 1248
失恋的感觉
失恋的感觉 2020-12-04 06:02

I\'ve implemented a REST/CRUD backend by following this article as an example: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/ . I h

相关标签:
3条回答
  • 2020-12-04 06:03

    You can implement this way

    $resource('http://localhost\\:3000/realmen/:entryId', {entryId: '@entryId'}, {
            UPDATE: {method: 'PUT', url: 'http://localhost\\:3000/realmen/:entryId' },
            ACTION: {method: 'PUT', url: 'http://localhost\\:3000/realmen/:entryId/action' }
        })
    
    RealMen.query() //GET  /realmen/
    RealMen.save({entryId: 1},{post data}) // POST /realmen/1
    RealMen.delete({entryId: 1}) //DELETE /realmen/1
    
    //any optional method
    RealMen.UPDATE({entryId:1}, {post data}) // PUT /realmen/1
    
    //query string
    RealMen.query({name:'john'}) //GET /realmen?name=john
    

    Documentation: https://docs.angularjs.org/api/ngResource/service/$resource

    Hope it helps

    0 讨论(0)
  • 2020-12-04 06:20

    I'm the creator of Restangular.

    You can take a look at this CRUD example to see how you can PUT/POST/GET elements without all that URL configuration and $resource configuration that you need to do. Besides it, you can then use nested resources without any configuration :).

    Check out this plunkr example:

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

    You could also see the README and check the documentation here https://github.com/mgonto/restangular

    If you need some feature that's not there, just create an issue. I usually add features asked within a week, as I also use this library for all my AngularJS projects :)

    Hope it helps!

    0 讨论(0)
  • 2020-12-04 06:22

    Because your update uses PUT method, {entryId: $scope.entryId} is considered as data, to tell angular generate from the PUT data, you need to add params: {entryId: '@entryId'} when you define your update, which means

    return $resource('http://localhost\\:3000/realmen/:entryId', {}, {
      query: {method:'GET', params:{entryId:''}, isArray:true},
      post: {method:'POST'},
      update: {method:'PUT', params: {entryId: '@entryId'}},
      remove: {method:'DELETE'}
    });
    

    Fix: Was missing a closing curly brace on the update line.

    0 讨论(0)
提交回复
热议问题