如何将 2 个模型绑定到 Angular 中的一个输入字段? [英] How to bind 2 models to one input field in Angular?
本文介绍了如何将 2 个模型绑定到 Angular 中的一个输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无论如何,我可以将两个模型值绑定到一个输入字段吗?
Is there anyway that I can bind two model values to one input field?
假设我有输入字段,我希望它是范围内两个变量的值,例如:
Suppose I have input field which I want to be the value of two variables in the scope something like:
<input type="text" model="sn_number; id" >
推荐答案
您不能,但有一些解决方法.
You cannot, but there are some workarounds.
<input type="text"
ng-model="sn_number"
ng-change="id=sn_number"/>
2.您可以观看一个模型,并在发生变化时更新另一个
$scope.$watch('sn_number', function(v){
$scope.id = v;
});
如果您想让它们保持同步,您还需要注意 id
中的变化.
You would need to watch also for changes in id
if you want to keep them in sync.
这篇关于如何将 2 个模型绑定到 Angular 中的一个输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文