2015-01-14 96 views
0

我沒有從角度服務中獲得一批客戶。如何正確地從角度服務返回承諾?

服務:

surchargeIndex.service('customerService', [ 
    "$http", function ($http) { 
     this.get = function (customerType) { 
      if (customerType == "1") { 
       getProduction().then(function (result) { return result.data; }); 
      } else if (customerType == "2") { 
       getTest().then(function (result) { return result.data; }); 
      } else { 
       getAll().then(function (result) { return result.data; }); 
      } 
     } 
var getTest = function() { 
     return $http({ 
      method: "GET", 
      url: "api/Customer/GetTest", 
     }) 
      .success(function (data) { 
       return data; 
      }); 
    }; 

    var getProduction = function() { 
     return $http({ 
      method: "GET", 
      url: "api/Customer/GetProduction", 
     }) 
      .success(function (data) { 
       return data; 
      }); 
    }; 

    var getAll = function() { 
     return $http({ 
      method: "GET", 
      url: "api/Customer/GetAll", 
     }) 
      .success(function (data) { 
       return data; 
      }); 
    }; 

控制器電話:

$scope.getCustomers = function(customerType) { 
     $scope.showContent = false; 
     $scope.customers = customerService.get(customerType); 
    }; 

HTML:

<div ng-controller="CustomerController" data-ng-init="init()" class="col-md-2"> 
    <select ng-model="customerKey" ng-options="customer.Key as customer.Value for customer in customers"></select> 
</div> 

的呼叫正在以每次服務器進行;但是,選擇列表未被填充。

+0

您可以發佈'getProduction()','getTest()'和'得到所有()'所以我們可以看到你回來了嗎?如果你想從'this.get()'返回,那麼你可以返回$ http方法並鏈接到它'return $ http()。then()..... something()。then()。然後()' –

+0

爲什麼大多數人不使用$資源? :sad: –

+0

這實際上是http://stackoverflow.com/questions/21807592/angular-js-promise-in-service-not-updating-view?rq=1的重複 - 你的問題是你是綁定promise而不是綁定其結果,解決方案是將綁定代碼附加到返回的promise的'then'處理程序。 – Rytmis

回答

2

添加return語句向客服get方法,也刪除),你都寫在GETALL(成功的功能,getProduction(),getTest()方法

surchargeIndex.service('customerService', [ 
    "$http", function ($http) { 
     this.get = function (customerType) { 
      var promise; 
      if (customerType == "1") { 
       promise = getProduction().then(function (result) { return result.data; }); 
      } else if (customerType == "2") { 
       promise = getTest().then(function (result) { return result.data; }); 
      } else { 
       promise = getAll().then(function (result) { return result.data; }); 
      } 
      return promise; 
     } 
    var getTest = function() { 
     return $http({ 
      method: "GET", 
      url: "api/Customer/GetTest", 
     }); 

    }; 

    var getProduction = function() { 
     return $http({ 
      method: "GET", 
      url: "api/Customer/GetProduction", 
     }); 
    }; 

    var getAll = function() { 
     return $http({ 
      method: "GET", 
      url: "api/Customer/GetAll", 
     }); 
    }; 

控制器

$scope.getCustomers = function(customerType) { 
     $scope.showContent = false; 
     customerService.get(customerType).then(function(data) { 
         $scope.customers = data; 
     }); 
    }; 
+0

這仍然不填充選擇列表。 – Robert

+0

@Robert對控制器代碼的這種編輯就是我上面提到的關於'then'處理程序的意思。 – Rytmis

+0

@Rytmis所以應該有兩個承諾。一個在控制器中,另一個在服務中? – Robert

0

添加更多return小號

surchargeIndex.service('customerService', [ 
"$http", function ($http) { 
    this.get = function (customerType) { 
     if (customerType == "1") { 
      return getProduction().then(function (result) { return result.data; }); 
      ^^^^^^ 
     } else if (customerType == "2") { 
      return getTest().then(function (result) { return result.data; }); 
     } else { 
      return getAll().then(function (result) { return result.data; }); 
     } 
    } 
+0

即使添加了回報,該列表尚未填充 – Robert