紫帆梓
使用promise模式:service代码app.service('IndexServ',function($http,$q){
this.getData = function(){ //暴露一个接口
//获取数据,并缓存数据
return $http.get('data/data.json',{cache:true}).then(function (d) {
this.data = d;
return $q.when(d);
}, function (d) {
return $q.reject(d);
});
};
});controller代码app.controller('IndexCtrl',function($scope,IndexServ){
IndexServ.getData().then(function(res){ //调用获取数据的接口
$scope.data = res.data;
});
}