1
我正在使用表單創建一個頁面,可以在哪裏修改作業。 當頁面加載下面的promise鏈時會觸發它獲取當前的工作數據並填充表單。有承諾的AngularJs ng選項問題
我在選擇城市時遇到問題,因爲只有在返回工作數據時,我纔會知道省和城市的價值。在這個階段,另一個承諾是建立城市選擇,但是當它被解決時,選擇的城市選擇的價值 - 這個城市選擇的城市 - 沒有被設置爲$ scope.job.city,不知道爲什麼。
api.getJobCategories()
.then(function (responseObject) {
$scope.jobCategories = responseObject.data;
return api.getJob({
job_id: $routeParams.job_id
});
})
.then(function (responseObject) {
$scope.job = responseObject.data;
// $scope.job.city has correct city value
return api.getProvinceCities({
province_name: $scope.job.province,
asJson: true
});
})
.then(function (responseObject) {
// Issue: $scope.job.city has correct value but after building
// the cities select $scope.job.city value is not applied to cities select
$scope.cites = responseObject.data;
});
模板
<form name="formjob" class="css-form" novalidate>
... more controls
<!-- Issue job.city model value does not bind to control -->
<select
ng-model="job.city"
ng-options="city.city_name as city.label for city in cites"
name="city"
class="form-control"
required="" >
</select>
</form>
可能的解決方法是緩慢,因爲必須等待所有的三個承諾是由JB Nizet其中工程
var job, city;
job = city = null;
api.getJobCategories()
.then(function (responseObject) {
$scope.jobCategories = responseObject.data;
return api.getJob({
job_id: $routeParams.job_id
});
})
.then(function (responseObject) {
job = responseObject.data;
city = job.city; // store city in temp var
job.city = null; // or delete job.city
$scope.job = job;
return api.getProvinceCities({
province_name: job.province,
asJson: true
});
})
.then(function (responseObject) {
$scope.cites = responseObject.data;
$scope.job.city = city; // reassign city
});
難道你只是將工作城市存儲在一個臨時變量中,並在加載城市後將其重新分配給作業? –
@JBNizet不是最優雅的解決方案,但它的工作表示感謝;)我更新了問題,以反映建議 – ericsicons
@ericsicons,我建議你回答自己的問題,而不是編輯問題,並讓它掛起而不回答,即使它在技術上解決 –