Angularjs - 如何在确认对话框为 false 时取消下拉菜单的更改事件?
Angularjs - How to cancel change event on dropdown when the confirm dialog is false?
我正在使用 Angularjs。我的问题是当我 select 在我的下拉列表中添加一个新选项时,会出现一个对话框。如果对话框的结果为 false,则 selected 下拉选项必须相同。将分析从其他开发人员那里获得的想法。提前致谢!
参考我下面的代码片段:
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="myDropDown" ng-change="Dialog()">
<option>a</option>
<option>b</option>
</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.Dialog = function () {
var dialog = confirm('Continue?');
if(!dialog) {
alert("Option must not change");
/** The problem is the dropdown still select the option. **/
}
}
});
</script>
这里有一个你可能不知道的技巧:
ng-change="dialog(myDropDown)" // pass the NEW value of myDropDown
ng-change="dialog('{{myDropDown}}')" // pass the OLD value of myDropDown
调用 dialog()
时,您可以将先前选择的选项作为参数传递。
然后,如果对话框被取消,就回滚它。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.dialog = function(prev) {
var dialog = confirm('Continue?');
if(!dialog) {
$scope.myDropDown = prev;
alert("Option must not change");
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="myDropDown" ng-change="dialog('{{myDropDown}}')">
<option>a</option>
<option>b</option>
</select>
</div>
我正在使用 Angularjs。我的问题是当我 select 在我的下拉列表中添加一个新选项时,会出现一个对话框。如果对话框的结果为 false,则 selected 下拉选项必须相同。将分析从其他开发人员那里获得的想法。提前致谢!
参考我下面的代码片段:
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="myDropDown" ng-change="Dialog()">
<option>a</option>
<option>b</option>
</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.Dialog = function () {
var dialog = confirm('Continue?');
if(!dialog) {
alert("Option must not change");
/** The problem is the dropdown still select the option. **/
}
}
});
</script>
这里有一个你可能不知道的技巧:
ng-change="dialog(myDropDown)" // pass the NEW value of myDropDown
ng-change="dialog('{{myDropDown}}')" // pass the OLD value of myDropDown
调用 dialog()
时,您可以将先前选择的选项作为参数传递。
然后,如果对话框被取消,就回滚它。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.dialog = function(prev) {
var dialog = confirm('Continue?');
if(!dialog) {
$scope.myDropDown = prev;
alert("Option must not change");
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="myDropDown" ng-change="dialog('{{myDropDown}}')">
<option>a</option>
<option>b</option>
</select>
</div>