一个输入字段上有多个 ng-models? [英] Multiple ng-models on one input field?
问题描述
我有一个表格和一个项目列表.我使用 ng-model="searchFor"
适当地过滤掉项目列表(这部分工作正常),但我也想提交"过滤掉的项目——这需要ng-model="adding_item.name"
在输入字段上也是如此(我认为).
你可以在一个输入字段上有多个 ng-models
吗?有没有其他办法解决这个问题?
尝试使用 ng-change 事件捕获模型值并将其分配给其他具有自己的 ng-model 的输入元素.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><div ng-app><input type="text" ng-model="input" ng-change="input1=input;input2=input;"/><input type="hidden" ng-model="input1"/><input type="hidden" ng-model="input2"/><br>模型<br>{{输入|大写}}<br>模型 1<br>{{输入1 |大写}}<br>模型 2<br>{{输入2 |大写}}
I have a form, and a list of items.
I used ng-model="searchFor"
to filter out the list of items appropriately (this part is working fine), but I also want to "submit" the item that's filtered out -- which would require ng-model="adding_item.name"
on the input field as well (I think).
Can you have multiple ng-models
on one input field?
Is there another way around this?
Try using ng-change event to capture model value and assign it to other input element with its own ng-model.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<input type="text" ng-model="input" ng-change="input1=input;input2=input; " />
<input type="hidden" ng-model="input1" />
<input type="hidden" ng-model="input2" />
<br>Model
<br>{{input | uppercase}}
<br>Model 1
<br>{{input1 | uppercase}}
<br>Model 2
<br>{{input2 | uppercase}}
</div>
这篇关于一个输入字段上有多个 ng-models?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!