在 ng-model 中可以连接吗?

Lar*_*ens 5 angularjs

只是一个简单的问题,是否可以将 ng-model 中名为“id”的字符串连接到 angularjs 中的变量 product.id?有没有办法像这样的方法实际上可以工作?

这有效:

ng-model="currentQuantity['id1']"
Run Code Online (Sandbox Code Playgroud)

这不起作用:

ng-model='currentQuantity[id + "" + product.id]'
Run Code Online (Sandbox Code Playgroud)

Tim*_*sen 5

一种可能的解决方法是使用二维变量来表示您的ng-model,即

ng-model='currentQuantity[id][product.id]'
Run Code Online (Sandbox Code Playgroud)


cha*_*tfl 5

您需要引用字符串id,否则它将被评估为范围变量

改变:

ng-model='currentQuantity[id + "" + product.id]'
Run Code Online (Sandbox Code Playgroud)

ng-model='currentQuantity["id" + product.id]'
Run Code Online (Sandbox Code Playgroud)