Javascript AngularJS-如何还原选择中的选定选项
我想将select中的选定选项还原为先前选定的值。在选择一个选项时,我从服务器调用中得到响应,该响应通过承诺得到解决。根据服务器的响应,我希望允许/不允许用户选择当前选择的选项Javascript AngularJS-如何还原选择中的选定选项,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,我想将select中的选定选项还原为先前选定的值。在选择一个选项时,我从服务器调用中得到响应,该响应通过承诺得到解决。根据服务器的响应,我希望允许/不允许用户选择当前选择的选项 app.controller('MainCtrl', function($scope, $q, $timeout) { $scope.options = [{value: 'abc'},{value: 'def'}]; $scope.select = undefined; var ol
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;
}
});
我尝试了下面提到的解决方案(),但没有用
我已将我的代码张贴在
任何想法/建议都会非常有用。我花了4-5个小时寻找解决方案
谢谢。使用定义拒绝(false)代码>而不是def.resolve(false)代码>