1 @{ 2 Layout = null; 3 } 4 5 <!DOCTYPE html> 6 7 <html> 8 <head> 9 <meta name="viewport" content="width=device-width" /> 10 <title>Resource</title> 11 <script src="~/Scripts/angular.min.js"></script> 12 <script src="~/Scripts/angular-resource.min.js"></script> 13 <script type="text/javascript"> 14 var m1 = angular.module("myApp", ["ngResource"]); 15 m1.controller("firstController", ["$scope", "$resource", function ($scope, $resource) { 16 var data = $resource(":name", { id: '@@id' }); //name 会被替换掉,传参用@符号 17 //也可以这样写 var data = $resource(":name",{name:getData});//第二个参数就是用来传参 18 // var data = $resource(":name",{id,@@id}, 19 //{getAge:{method:"get",params:{age:'20'},isArray:false}, 20 //getSex:{method:"get",params:{sex:'男'},isArray:false} 21 //});//第三个参数就是用来自定义方法的isArray为true表示是query如果是false表示是get,第三个参数可以自定义多个方法 22 23 24 //支持的方法 25 /* 26 { 27 "get":{method:"get"}, 28 "save":{method:"post"}, 29 "query":{method:"get",isArray:true}, 30 "remove":{method:"delete"}, 31 "delete":{method:"delete"} 32 } 33 */ 34 35 $scope.data = data.get({ "name": "getData" }, { id: 20 }, function (data) {//Get请求并传参 ,GET只能取一条数据 36 37 }); 38 39 $scope.data1 = data.query({ "name": "getDataList" }, function (data) {//查询多条就得用query 40 41 }); 42 43 //第三种方法 44 // $scope.data2 = data.getAge({ id: 21 }); 45 46 console.log(data); 47 48 //使用GET请求去请求数据 49 // console.log(obj); 50 // var objRe = $resource("", {}); 51 52 53 }]); 54 </script> 55 </head> 56 <body> 57 <div ng-app="myApp"> 58 <div ng-controller="firstController"> 59 {{data.name}} 60 {{data.age}} 61 62 <ul> 63 <li ng-repeat="d in data1"> 64 {{d.name}}-{{d.age}} 65 66 </li> 67 </ul> 68 </div> 69 </div> 70 </body> 71 </html>