AngularJS - $resource different URL for Get and Post

前端 未结 5 1484
南笙
南笙 2020-12-24 14:35

$resource is awesome providing very convenient way to handle web services. What if GET and POST have to be performed on different URLs?

For example, GET URL is

5条回答
  •  一向
    一向 (楼主)
    2020-12-24 15:00

    Follow this way:

    (function () {
        'use strict';
    
        angular
            .module("app")
            .factory("SomeFactory", SomeFactory);
    
        function SomeFactory($resource) {
            var provider = "http://stackoverflow.com/:action/:id";
            var params = {"id":"@id"};
            var actions = {
                "create":   {"method": "POST",  "params": {"action": "CreateAwesomePost"}},
                "read":     {"method": "POST",  "params": {"action": "ReadSomethingInteresting"}},
                "update":   {"method": "POST",  "params": {"action": "UpdateSomePost"}},
                "delete":   {"method": "GET",   "params": {"action": "DeleteJustForFun"}}
            };
    
            return $resource(provider, params, actions);
        }
    })();
    

    I hope it help! Enjoy!

提交回复
热议问题