標籤:$resource
備忘:個人小結,僅供參考,歡迎糾正
1、注入resource
var studentService =angular.module(‘studentServiceApp‘, [
‘ngAnimate‘,
‘ngCookies‘,
‘ngResource‘
]);
2、
studentService.config([‘$httpProvider‘,‘app‘,
function ($httpProvider) {
$httpProvider.defaults.headers.common[‘Authorization‘]="";
}
])
3、寫服務可訪問伺服器上的資料也可以訪問本機資料
studentService.factory(‘restAPI‘, [‘$resource‘,function ($resource) {
var ip="http:/v1";
return {
citys: $resource(ip+‘/city‘),//訪問其他主機資料
studentbyname: $resource(ip+‘/studentinformation/:id‘,{id:‘@id‘}),
provinceandcitys:$resource(‘../json/ProvinceAndCityJson.json‘),//訪問本機資料
};
}
]);
4、注入到Controler中
student
var studentAPI=app.restAPI.studentbyname;
//帶參數
studentAPI.get({id:**},function(data){
//data為伺服器上的資料或者本機資料
},function(data){
//data
});
//不帶參數
app.restAPI.provinceandcitys.get( function (data) {
$scope.citys=data.province;
}, function (data) {
});
}
angularjs $resource用法小結