gpt4 book ai didi

javascript - 将 ng-model 数组绑定(bind)到固定数量的文本框

转载 作者:行者123 更新时间:2023-11-29 21:14:48 24 4
gpt4 key购买 nike

如何将数组形式的 ng-model 绑定(bind)到固定数量的文本框?

假设我有三个文本框,它们都是可选的,您如何将 ng-model 绑定(bind)到这些文本框?

<input type="text" name="optionalField[]">
<input type="text" name="optionalField[]">
<input type="text" name="optionalField[]">

您可以在任何文本框(例如,仅文本框 2 和 3)中输入内容,当您保存它时,它将变成 optionalField: ['text2', 'text3']

放置文本的位置无关紧要;重要的是文本框的顺序。因此,当您再次显示 View 时,它只会绑定(bind)到第一个和第二个文本框。如果您只在文本框 3 上输入,它将绑定(bind)到文本框 1,依此类推。

更新:

刚用过dfsq的答案并将其修改为返回经过过滤的值数组。 http://codepen.io/anon/pen/ozpxGq?editors=1011

最佳答案

ng-repeat 中你可以使用 $index

<input type="text" name="optionalField[]" ng-model="optionalField[$index]">

关于javascript - 将 ng-model 数组绑定(bind)到固定数量的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39869734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com