我沒有從角度服務中獲得一批客戶。如何正確地從角度服務返回承諾?
服務:
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>
的呼叫正在以每次服務器進行;但是,選擇列表未被填充。
您可以發佈'getProduction()','getTest()'和'得到所有()'所以我們可以看到你回來了嗎?如果你想從'this.get()'返回,那麼你可以返回$ http方法並鏈接到它'return $ http()。then()..... something()。then()。然後()' –
爲什麼大多數人不使用$資源? :sad: –
這實際上是http://stackoverflow.com/questions/21807592/angular-js-promise-in-service-not-updating-view?rq=1的重複 - 你的問題是你是綁定promise而不是綁定其結果,解決方案是將綁定代碼附加到返回的promise的'then'處理程序。 – Rytmis