简体   繁体   English

角度,在文本框中显示选定的选项

[英]Angular, show selected option in textbox

I have a multiple columns table and showing their values in multiple controls by Angular JS. 我有一个多列表,并通过Angular JS在多个控件中显示它们的值。 For example I am using Select/Option (multiple) to show column A, and I want to use another text box to show column B, based on select/option. 例如,我正在使用“选择/选项(多个)”来显示列A,并且我想基于选择/选项使用另一个文本框来显示列B。 Here is code: 这是代码:

Column A in Select (multiple): 选择(多个)中的A列:

<select multiple data-ng-model="selectedEmp" data-ng-options="o.emp for o in post.Emps" >
    <option value="SampleValue">Samplevalue</option>
</select>

Column B in text box: 文本框中的B列:

<input data-ng-model="selectedEmp.gender" type="text"  style="width:60px;"> </input>

The thing is, when I am not using 'Multiple' in select control, the gender value (in same record) can be displayed in text box, but when using 'multiple', the text box is not showing selected current record. 问题是,当我在选择控件中未使用“多个”时,性别值(在同一记录中)可以显示在文本框中,但是当使用“多个”时,文本框未显示所选的当前记录。

Please help on how to implement this. 请帮助如何实现这一点。

Thanks 谢谢

When you are using select without using multiple , you are binding to a object. 当您使用select而不使用multiple ,您将绑定到一个对象。 So your selectedEmp might be something like {"emp":1,"gender":"M"} (an object). 因此,您的selectedEmp可能类似于{"emp":1,"gender":"M"} (一个对象)。

But when you use multiple select, you are binding to an array. 但是,当您使用multiple选时,您将绑定到数组。 So your selectedEmp might be something like : [{"emp":1,"gender":"M"},{"emp":2,"gender":"F"}] . 因此,您的selectedEmp可能类似于: [{"emp":1,"gender":"M"},{"emp":2,"gender":"F"}] Notice the [] . 注意[]

So, in case of multiple , you also need to pass the index whose value you want to bind. 因此,如果为multiple ,则还需要传递要绑定其值的索引。 But in this case, it seems like you want to have input 's for multiple selected items from above select . 但是在这种情况下,似乎您想从select上面为多个选定项input

I would use something like this: 我会用这样的东西:

<input data-ng-model="s.gender" type="text" ng-repeat="s in selectedEmp" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM