ng-change 获取新值和原值 [英] ng-change get new value and original value

查看:59
本文介绍了ng-change 获取新值和原值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ng-options 从下拉列表中选择值.我希望能够将旧值与新值进行比较.ng-change 可以很好地抓取下拉的新值,但是如何同时获取新值和原始值?

I'm using ng-options to select values from a pulldown. I'd like to be able to compare the old value to the new value. ng-change works well for grabbing the new value of the pull down, but how can I get both the new value and the original value?

<select ng-change="updateValue(user)" ng-model="user.id" ng-options="user.id as user.name for user in users"></select> 

例如,假设我希望控制器记录您以前的用户名是 BILL,您当前的用户名是 PHILLIPE."

For instance, let's say I wanted the controller to log, "Your former user.name was BILL, your current user name is PHILLIPE."

推荐答案

使用 angular {{expression}},您可以将旧的 user 或 user.id 值作为文字字符串添加到 ng-change 属性:

With an angular {{expression}} you can add the old user or user.id value to the ng-change attribute as a literal string:

<select ng-change="updateValue(user, '{{user.id}}')" 
        ng-model="user.id" ng-options="user.id as user.name for user in users">
</select>

在 ngChange 上,updateValue 的第一个参数将是新的用户值,第二个参数将是选择标签最后一次通过 angular 更新时形成的文字,使用旧的 user.id 值.

On ngChange, the 1st argument to updateValue will be the new user value, the 2nd argument will be the literal that was formed when the select-tag was last updated by angular, with the old user.id value.

这篇关于ng-change 获取新值和原值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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