本文介绍了用户从下拉列表框中选择选项后,如何获取新选择的值和旧的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有angularjs下拉列表框:
I have angularjs dropdown list box:
<select class="form-control"
ng-model="editor.frequency"
ng-options="item as item.Name for item in editor.frequencies"
ng-change="editor.editFrequencyManual = false;">
</select>
在用户单击下拉列表框中的选项后,我需要获取被单击(选定)的值和旧值(在选定之前).
After user click on option in dropdown list box I need to get clicked(selected) value and the old value(before selection).
任何想法我怎么能得到这两个值?我应该使用事件函数吗?
Any idea how can I get this two values?Is there event function that I sholud use?
推荐答案
您可以查看模型上的更改:
You could watch the changes on your model:
$scope.$watch('editor', function(newValue, oldValue){
//
});
或者您可以修改 ng-change
,以使其将新旧值作为参数传递:
Or you could modify ng-change
so that it passes old and new values as parameters:
<select class="form-control"
ng-model="editor.frequency"
ng-options="item as item.Name for item in editor.frequencies"
ng-change="changed(editor.frequency, {{editor.frequency}}); editor.editFrequencyManual = false;">
</select>
当然,您应该将 editor.editFrequencyManual = false;
移到更改函数中.
Of course, you should move editor.editFrequencyManual = false;
into the change function.
function myController($scope) {
$scope.myModel = {};
$scope.doThis = function(newValue, oldValue) {
console.log("New value: " + newValue);
console.log("Old value: " + oldValue);
}
}
angular.module('myApp', []);
angular
.module('myApp')
.controller('myController', myController);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myController">
<select ng-model="myModel" ng-change="doThis(myModel, '{{myModel}}');">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
这篇关于用户从下拉列表框中选择选项后,如何获取新选择的值和旧的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!