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]'
You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.
In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination).
The concatenation operators combine two strings to form one string by appending the second string to the right-hand end of the first string.
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]'
One possible workaround would be to use a two dimensional variable to represent your ng-model
, i.e.
ng-model='currentQuantity[id][product.id]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With