ngResource快速上手

废话不多说,本文的目的就是看过之后就会使用ngResource了。

  1. 引入angular-resource.min.js
  2. 定义模块时加载ngResource
  3. 定义service

完整代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var app = angular.module('app', ["ngResource"]);
app.factory('Phone', ['$resource',
function($resource) {
return $resource('user/:uid', {
uid: '@uid'
},
{
update: {
method: 'PUT'
}
});
}]);
app.controller('MainCtrl', ['$scope', 'Phone',
function($scope, Phone) {
$scope.phones = Phone.query();
Phone.update({
uid: 1
},
{
name: "zhangsan",
age: "10"
});
}]);

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×