ngModel and ngFor

Yonathan Meguira
1 min readOct 23, 2017

Today I kinda banged my head against a silly issue.

How Do I manage to have something like this ?

Angular is not really fan of us using [(ngModel)] inside *ngFor because of the computational ‘cost’ going on under the hood.

Anyways, sometimes it is just needed and you have no other way than to use this kind of pattern.

If you feel the urge to say that this is anti-design-pattern, write that on a piece of paper, fold it twice, then thrice then swallow it and hopefully chock to death (don’t do that!)

Let’s assume that your class contains such an array. Ideally, I would want to loop through the array and use each of its term as a model for an input in my DOM. (Complicated way of explaining the first gist up here).

Now, if you do that like a wide Javascript Monkey you might get slapped on the face by the Angular Police (does not actually exist).

Your CLI will start bitching saying

“Uncaught Error: Cannot assign to a reference or variable!”

The solution ? (too lazy to explain it so here is another gist, sorry for you out there reading on your phone)

--

--