ngModel not working Ionic 2


Can Anyone tell me How can I use ngmodel if I am binding data like this:

 for (var i = 0; i < this.lines.length; i++) {
            var textToAdd = "<ion-item (ngModel)="model_from_ts_file" id=\"tr" + i + "\">" +
                "<p id=\"td" + this.lines[i][2] + "\">" +
                this.lines[i][1] +
                "</p>" +

            // console.log(textToAdd)


Don’t build templates in controller code. Write templates in template files.


I wants it before like this but now I have done it in a different way. Thanks.


The point of support forums is to share knowledge. Can you explain what you ended up doing, so that others can benefit?


Shouldn’t it be “[(ngModel)]=” ?


No, That is old way.


I will share. I have done like as you said before. Using *ngFor.


Oh, okay! The latest angular docs show the syntax I referred to for two-way data binding so I got confused.


Box binding flows from controller to view:

<button [disabled]="!form.valid">

Banana binding flows from view to controller:

<button (click)="frobulate()">

Banana-in-a-box binding goes both ways:

<input [(ngModel)]="fruit">

It is actually syntactic sugar for:

<input [ngModel]="fruit" (ngModelChange)="fruitChanged($event)">

…where Angular creates the change handler for you that updates this.fruit in the controller. So when you want to enable two-way binding in custom components, you need a corresponding @Output() fooChange: EventEmitter for every @Input() foo.

Passing Value of an Input type text to the home.ts

What I probably wrongly thought was that whenever we were using 'ngModel' we wanted to bind in both direction so the '[( .. )]' syntax was necessary.

Thanks for the explanation!


I would not consider that wrong, so carry on.


Oh, then I am again confused :smiley: So, did @HarinderSingh actually code it wrongly or what?


We’ll never know until they come back and post how they ended up resolving it, but given no other information, I firmly agree with your suggestion of:

Shouldn’t it be “[(ngModel)]=” ?

and disagree with the dismissal of that suggestion as “old way”.


Hm I get that, thanks!


Hmm but I am using ngModel like (ngModel) and it works like charm


And the data is bound two-way even with this?


this is not old way…It is wrong way
Old way is ng-model not ngModel