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
HarinderSingh:
(ngModel)=
Shouldnât it be â[(ngModel)]=
â ?
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
KishuPro:
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
.
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 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
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