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>" +
                "</ion-item>";

            $("#list").append(textToAdd);
            // console.log(textToAdd)
        }

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

2 Likes

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?

1 Like

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

1 Like

No, That is old way.

1 Like

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.

1 Like

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.

1 Like

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!

1 Like

I would not consider that wrong, so carry on.

2 Likes

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

1 Like

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

2 Likes

Hm I get that, thanks!

1 Like

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

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

1 Like

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

2 Likes