如何检测,当用户在选择下拉使用AngularJS改变的价值? [英] How can I detect when a USER changes the value in a select drop down using AngularJS?

查看:95
本文介绍了如何检测,当用户在选择下拉使用AngularJS改变的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

<select data-ng-model="selectedTestAccount" data-ng-options="item.Id as item.Name for item in testAccounts">
   <option value="">Select Account</option>
</select>

有人提议,我认为我可以看这个选择的值与以下更改:

It was suggested to me that I could watch for the value of this select to change with the following:

$scope.$watch('testAccounts', function(){
  /* get updated values and update other select "model" on success*/
  alert("hello");
});

不过,这似乎并没有工作。一方面它只要选择出现在屏幕上,用户选择任何事情之前给出了一个警告。另外我是纠正说,我应该看在selectedTestAccount改变?最后一个问题。我怎样才能显示selectedTestAccount的价值在一个警告框?

But this does not seem to work. For one thing it gives an alert as soon as the select appears on the screen and before the user has selected anything. Also am I correct in saying that I should be watching for a change in selectedTestAccount? Finally one more question. How can I show the value of selectedTestAccount in an alert box?

推荐答案

至于说,你可以把它作为第一个参数,无需从范围得到它。使用

As said, you can get it as the first argument, no need to get it from scope. Use

$scope.$watch('selectedTestAccount', function(newValue){
  alert(newValue);
});

当心你总会得到未定义作为第一个变化的 $观看

这篇关于如何检测,当用户在选择下拉使用AngularJS改变的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆