angularjs设置promise将数据返回到变量

时间:2015-05-16 10:28:28

标签: javascript angularjs angular-promise

在为restful api中的数据分配范围变量时,我发现我的自我重复了很多。

示例:

.controller('wallCtrl',function($scope,$http,server) {
  $scope.facilities = [];
  $http.get(server+'/facilities').success(function(data){$scope.facilities = data;)});

代码

的另一个例子
.controller('wallCtrl',function($scope,$http,server) {
  $scope.facilityX = [];
  $http.get(server+'/facilities').success(function(data){$scope.facilityX = data.filter(function(e){return e.type=='X';};)});

或者

.controller('wallCtrl',function($scope,$http,server) {
  $scope.facilityX = [];

  //get called somewhere in my view
  $scope.updateFacility = function(){
    $http.get(server+'/facilities').success(function(data){$scope.facilityX = data;});
  }

这样做时我应该遵循一个快捷方式/清洁方法吗?

0 个答案:

没有答案