Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation possible in ng-model?

Tags:

angularjs

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]'
like image 201
Lars Mertens Avatar asked Sep 26 '16 14:09

Lars Mertens


People also ask

How do I combine multiple strings into one?

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.

Which string function is use for concatenation?

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).

What is the concatenation of string holds?

The concatenation operators combine two strings to form one string by appending the second string to the right-hand end of the first string.


2 Answers

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]'
like image 115
charlietfl Avatar answered Oct 07 '22 07:10

charlietfl


One possible workaround would be to use a two dimensional variable to represent your ng-model, i.e.

ng-model='currentQuantity[id][product.id]'
like image 40
Tim Biegeleisen Avatar answered Oct 07 '22 07:10

Tim Biegeleisen