ngModel not working Ionic 2


#1

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

#2

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


#3

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


#4

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


#5

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


#6

No, That is old way.


#7

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


#8

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


#9

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
#10

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!


#11

I would not consider that wrong, so carry on.


#12

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


#13

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


#14

Hm I get that, thanks!


#15

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


#16

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


#17

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