0
我想將select中的選定選項恢復爲先前選定的值。在選擇一個選項時,我從服務器調用中獲得響應,並通過承諾解決。根據服務器的響應,我想允許/禁止用戶選擇當前選擇的選項。AngularJS - 如何恢復選中的選定選項
app.controller('MainCtrl', function($scope, $q, $timeout) {
$scope.options = [{value: 'abc'},{value: 'def'}];
$scope.select = undefined;
var oldSelect;
$scope.confirmChange = function() {
console.log(arguments);
confirmDialog($scope.select).then(function(val) {
console.log(val);
oldSelect = $scope.select;
},
function() {
$timeout(function() {
$scope.select = oldSelect;
}, 10);
});
}
var confirmDialog = function(newVal) {
// obviously not a good way to ask for the user to confirm
// replace this with a non blocking dialog
//the timeout is only for the confirm since it's blocking the angular $digest
var def = $q.defer();
c = confirm('Is it ok? [' + newVal.value + ']');
if(c) {
def.resolve(true);
}
else {
def.resolve(false);
}
return def.promise;
}
});
我試過按照這裏提到的解決方案(SO post),但沒有用。
我已經張貼了我的plunkr
任何想法碼/指針將是很有益的。我花了4-5個小時尋找解決方案。
謝謝。