zoukankan      html  css  js  c++  java
  • restangular一点用法小记录

    1.Restangular依赖Lo-Dash或Underscore,因此为了确保Restangular可以正常运行,需要引入这两个库中的一个。

    <script type="text/javascript" src="js/angular.js"></script>
    <script type="text/javascript" src="js/lodash.js"></script>
    <script type="text/javascript" src="js/restangular.js"></script>

    2.项目中用Restangular:

    var app=angular.module("myApp",["restangular"]);//此处的restangular首字母必须小写,否则报错
    app.controller("myCtrl",function($scope,Restangular){}//此处的restangular首字母必须大写,否则报错

    3.Restangular的方法测试

    前端请求页面

    app.controller("myCtrl",function($scope,Restangular){
        //get请求
        Restangular.all("get").get({}).then(function(data){
            console.log(JSON.stringify(data));//[{"name":"张飞111"},{"name":"关羽111"}]
      })
        Restangular.all("get",147).get({}).then(function(data){
            console.log(JSON.stringify(data));//[{"name":"张飞111"},{"name":"关羽111"}]
        })
        Restangular.one("get",123).get({}).then(function(data){
            console.log(JSON.stringify(data));//[{"name":"刘备"},{"name":"曹操"}]
      })
    
        //post请求
        Restangular.all("post").post().then(function(data){
            console.log(JSON.stringify(data));//[{"age":"27"}]
       })
        Restangular.one("post",456).post().then(function(data){
            console.log(JSON.stringify(data));//[{"age":"29"}]
        })
    })

    后端处理请求页面

    router.get('/get', function(req, res, next) {
     console.log(req.query);//获取直接get方式附在url?之后key=value形式的参数 xxx.xx?y=x res.send(
    '[{"name":"张飞111"},{"name":"关羽111"}]'); }); router.get('/get/147', function(req, res, next) { res.send('[{"name":"张飞147"},{"name":"关羽147"}]'); }); router.get('/get/123', function(req, res, next) { res.send('[{"name":"刘备"},{"name":"曹操"}]'); }); router.post('/post', function(req, res, next) { res.send('[{"age":"27"}]'); }); router.post('/post/456', function(req, res, next) { res.send('[{"age":"29"}]'); });

    打印结果:

    总结:

      ①、Restangular的All方法会让所有的HTTP请求将首个字符串作为路径来请求数据,第二个字符串将不起作用。例如Restangular.all("get",147)请求路径实际还是'/get'

      ②、Restangular的One方法通过单个对象来发送嵌套的请求,路径按参数顺序用"/"分隔,例如Restangular.one("get",123)请求路径就是'/get/123'

      ③、Restangular是get请求还是post请求与all或one方法无关,与all或one方法后的get或post方法有关。

      ④、Restangular.all("get").get("")就是get请求,get方法内传的是请求的对象,对应的是url?后的参数值,如{"name":"lily"}则表示get请求的url为xxx?name=lily,如果是空对象,就表示不传参,如果不传对象,传字符串,Restangular会自动分隔解析成对象的形式。例如get("555"),会被解析成{"0":5,"1":5,"2":5}。如果不传参数,也会被解析成传的空对象。

      ⑤、Restangular.all("post").post()是post请求。

  • 相关阅读:
    Codeforces 294B Shaass and Bookshelf:dp
    Codeforces 372B Counting Rectangles is Fun:dp套dp
    Codeforces 402D Upgrading Array:贪心 + 数学
    Codeforces 571B Minimization:dp + 贪心【前后相消】
    Codeforces 509F Progress Monitoring:区间dp【根据遍历顺序求树的方案数】
    codeforces 447E or 446C 线段树 + fib性质或二次剩余性质
    类斐波那契数列的一些性质
    CF 1097D
    最近点对问题
    2018ACM-ICPC EC-Final 现场赛I题 Misunderstanding...Missing 倒着DP
  • 原文地址:https://www.cnblogs.com/iagw/p/6600777.html
Copyright © 2011-2022 走看看