Last active
March 17, 2016 02:31
-
-
Save guo-yu/5908791 to your computer and use it in GitHub Desktop.
使用Angular进行ngResource模块的注入,并组织一个Store模块进行数据的获取,写入和分析
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html(ng-app="app") | |
head | |
title demo | |
body | |
#wrap(ng-controller="myCtrl") | |
h1 我是 {{me.name}} , 我已经 {{me.age}} 岁了 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 注册app | |
var app = angular.module('app', ['store']); | |
// 定义这个数据仓库 | |
var store = angular.module('store', ['ngResource']).factory('Store', function($resource) { | |
return { | |
// define user api | |
user: $resource('/user/:id', {id:'@uid'}), | |
// define article api | |
article : $resource('/article/:id', {id:'@aid'}), | |
// define jsonp api | |
remote : $resource('/remoteData/:id', {id:'@rid'},{ | |
jsonp :{ | |
method:'JSONP', | |
params:{ | |
yourKey: 'yourValue' | |
} | |
} | |
}) | |
} | |
}) | |
// 在ctrler里使用这个Store获取数据 | |
var myCtrl = function($scope, Store) { | |
// fetch a user target | |
var user = Store.user.get({uid:123}, function() { | |
// 填入数据 | |
$scope.me = user; | |
// 写入新数据 | |
user.abc = true; | |
// 会执行post请求 | |
user.$save(); | |
// console.log this user | |
console.log(user) | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
是这样的:
解释几个疑问:
/article/:id'
,这个 :id 如果你用过express ,会发现这是一个placeholder,这个placeholder 在哪里使用呢?下边这个uid,就是我们刚才声明的placeholder. 本质是为了在使用原型方法的时候传参。