Lars Mertens Lars Mertens - 2 months ago 27
AngularJS Question

Concatenation possible in ng-model?

Just a simple question is it possible to concatenate a string called "id" inside the ng-model to variable product.id in angularjs? Is there a way a approach like this could actually work?

This works:

ng-model="currentQuantity['id1']"


This doesn't work:

ng-model='currentQuantity[id + "" + product.id]'

Answer

You need to quote the string id or it will be evaluated as a scope variable

Change:

ng-model='currentQuantity[id + "" + product.id]'

to

ng-model='currentQuantity["id" + product.id]'