zoukankan      html  css  js  c++  java
  • angular $resource 的 get请求 和 post请求

    1.语法:

    $resource(url,[paramDefaults],[actions],options);

    详解:

    (1)url:一个参数化的url模板

    (2)paramDefaults:url参数的默认值

    (3)actions: 用户对于resource行为的默认设置进行扩展的自定义配置的散列,该配置将会以$http.config的格式创建。

    (4)options:扩展$resourceProvider行为的自定义设置

    2.五种默认行为

    {
      "get":{method:"get"},
      "save":{method:"save"},
      "query":{method:"get",isArray:true},
      "remove":{method:"delete"},
      "delete":{method:"delete"}
    }
    
    get([params],[success],[error]);
    
    save([params],postData,[success],[error]);
    
    query([params],[success],[error]);
    
    remove([params],postData,[success],[error]);
    
    delete([params],postData,[success],[error]);
    
    $save([params],[success],[error]);
    
    $remove([params],[success],[error]);

    3.demo

    (function () {
      angular.module("Demo", ["ngResource"])
      .controller("testCtrl", ["$resource",testCtrl]);
      function testCtrl($resource) {
        var myResource = $resource("/url/_url", {}, {
          myPost: {
            method: "post",
            url: "/newUrl/_newUrl",
            params: { id: "4" },
            interceptor: {
              response: function (d) {
                console.log(d);
              },
              responseError: function (d) {
                console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
              }
            }
          }
        });
        myResource.get({ id: "1" }, function (d) {
          console.log(d);
        }, function (d) {
          console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
        });
        myResource.query({ content: "text" }, function (d) {
          console.log(d);
        }, function (d) {
          console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
        });
        myResource.save({ text: "Hello World" }, { text: "Hello World" }, function (d) {
          console.log(d);
        }, function (d) {
          console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
        });
        myResource.remove({ text: "Hello World" }, { text: "Hello World" }, function (d) {
          console.log(d);
        }, function (d) {
          console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
        });
        myResource.delete({ text: "Hello World" }, { text: "Hello World" }, function (d) {
          console.log(d);
        }, function (d) {
          console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
        });
        var newResource = new myResource();
        newResource.$save({ id: "2" }, function (d) {
          console.log(d);
        }, function (d) {
          console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
        });
        newResource.$remove({ id: "3" }, function (d) {
          console.log(d);
        }, function (d) {
          console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
        });
        myResource.myPost();
      };
    }());

    .

  • 相关阅读:
    final详解
    静态与非静态
    静态内部类详解
    iso-----genisoimage/md5sum命令用法
    Docker-----版本选择
    Docker-----deepin系统下docker安装registry
    shell高级-----正则表达式
    kolla-ansible-----rally模块
    shell高级-----创建函数
    jenkins入门-----(1)安装、配置
  • 原文地址:https://www.cnblogs.com/crazycode2/p/8853985.html
Copyright © 2011-2022 走看看