如果有人对我的问题的措辞有更好的建议,请编辑。
我有这个案例:
<input type="text" ng-model="panels[ <whats inside the input> ]"/>
我想让这个模型绑定到面板数组,其中数组的位置由某人在输入中键入的内容确定。
例子:
如果有人在输入中输入 3 我希望 ng-model 成为 panel[2] ,有没有办法做到这一点?
如果有人对我的问题的措辞有更好的建议,请编辑。
我有这个案例:
<input type="text" ng-model="panels[ <whats inside the input> ]"/>
我想让这个模型绑定到面板数组,其中数组的位置由某人在输入中键入的内容确定。
例子:
如果有人在输入中输入 3 我希望 ng-model 成为 panel[2] ,有没有办法做到这一点?
你混淆了什么ng-model
。 ng-model
是输入绑定的内容。因此,无论您输入什么,都会进入ng-model
. 通过根据键入的内容分配ng-model
索引,您实际上是在要求ng-model
绑定到两个不同的事物。
这是对您所要求的内容的看法。您可以通过绑定到第二个输入的变量来索引您的值:
<div ng-app ng-controller="MyCtrl">
<input ng-model="index" />
<input ng-model="panels[index]" />
{{panels}}
</div>
JS:
function MyCtrl($scope) {
$scope.panels = [ "one", "two", "three" ];
}