最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 5 ngModel doesn't update value - Stack Overflow

programmeradmin0浏览0评论

I have the following ponent:

export class ModuleComponentComponent implements OnInit {
    dropzoneConf;
    fileService = environment.getFileUrl;

    constructor(
        private moduleComponentService: ModuleComponentService) {
    }

    @Input()
    selectedComponent: ModuleComponent;

    ngOnInit() {
        this.setDropZoneConfig();
    }    
}

And in that I have the following HTML:

<h3 class="m-portlet__head-text m--font-success">
   <input class="form-control" type="text" [ngModel]="selectedComponent.title" />
</h3>

and the way I add the ponent in my HTML:

<div class="col-lg-8 col-x1-12" *ngIf="selectedComponent != null">
   <app-module-ponent [selectedComponent]="selectedComponent"></app-module-ponent>
</div>

When I type something into the input field it doesn't update the selectedComponent.title variable

What might be going on?

I have the following ponent:

export class ModuleComponentComponent implements OnInit {
    dropzoneConf;
    fileService = environment.getFileUrl;

    constructor(
        private moduleComponentService: ModuleComponentService) {
    }

    @Input()
    selectedComponent: ModuleComponent;

    ngOnInit() {
        this.setDropZoneConfig();
    }    
}

And in that I have the following HTML:

<h3 class="m-portlet__head-text m--font-success">
   <input class="form-control" type="text" [ngModel]="selectedComponent.title" />
</h3>

and the way I add the ponent in my HTML:

<div class="col-lg-8 col-x1-12" *ngIf="selectedComponent != null">
   <app-module-ponent [selectedComponent]="selectedComponent"></app-module-ponent>
</div>

When I type something into the input field it doesn't update the selectedComponent.title variable

What might be going on?

Share Improve this question edited Mar 2, 2022 at 10:51 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jun 13, 2018 at 12:26 Marc RasmussenMarc Rasmussen 20.6k83 gold badges223 silver badges384 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Use the two way binding

 [(ngModel)]="selectedComponent.title"

We need to use two way data binding with [(ngModel)]

<h3 class="m-portlet__head-text m--font-success">
   <input class="form-control" type="text" [(ngModel)]="selectedComponent.title" />
</h3>

You should read the part about two way data binding on Angular documentation

If you want to use [ngModel] only, you could but you have to catch changes with (ngModelChange)

<h3 class="m-portlet__head-text m--font-success">
   <input class="form-control" type="text" [ngModel]="selectedComponent.title" (ngModelChanges)="setTitle($event)" />
</h3>

You could improve it with forms, just ask me for any questions about that

you should use two-way data binding

[(ngModel)]

<input class="form-control" type="text" [(ngModel)]="selectedComponent.title" />

and make sure to import forms module in app.module.ts

import { FormsModule } from '@angular/forms';
发布评论

评论列表(0)

  1. 暂无评论