在一个输入字段上有多个ng模型? [英] Multiple ng-models on one input field?

查看:65
本文介绍了在一个输入字段上有多个ng模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表格和一份物品清单. 我使用ng-model="searchFor"来适当地过滤出项目列表(这部分工作正常),但我也想提交"已过滤出的项目-在输入字段上也需要ng-model="adding_item.name"(我思考).

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).

在一个输入字段上可以有多个ng-models吗? 还有其他解决方法吗?

Can you have multiple ng-models on one input field? Is there another way around this?

推荐答案

尝试使用ng-change事件捕获模型值,并将其分配给具有自己的ng-model的其他输入元素.

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模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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