简体   繁体   English

ngModel 使用 Angular8 时未显示加载的数据

[英]loaded data is not showing when ngModel is using Angular8

Initially when I used {{item.name}} to load the value it worked perfectly, but after I added ngModel it stopped working properly.最初,当我使用{{item.name}}加载值时,它运行良好,但在添加ngModel它停止正常工作。 Now no value is showing and whenever I enter anything in one text box the other text boxes also get filled with the same value现在没有显示任何值,每当我在一个文本框中输入任何内容时,其他文本框也会填充相同的值

please help me to solve this,请帮我解决这个问题,

HTML Code HTML代码


<tr *ngFor="let item of this.familyInfo">
  <td><input type="text" class="form-control" value="{{item.name | titlecase}}" [(ngModel)]="this.cr.name"></td>
  <td><input type="text" class="form-control" value="{{item.age}}" [(ngModel)]="this.cr.age"></td>
  <td><input type="text" class="form-control" value="{{item.relation | titlecase}}" [(ngModel)]="this.cr.relation"></td>
  <td><button class="style_blue_button_square" style="width: 100%;" id={{item.id}}
      (click)="updateFamilyInfo($event)">Udate</button></td>
</tr>

you need to add "FormsModule" to your app.module.ts file您需要将“FormsModule”添加到您的 app.module.ts 文件中

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import {FormsModule} from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

I reviewed you the above code, too.我也审查了你上面的代码。

[(ngModel)]="this.cr.name"

at there, this.在那里,这个。 was wrong.错了。 "this." “这个。” don't need.不需要。 you need to fix like the following.你需要像下面这样修复。

[(ngModel)]="cr.name"

When using ngModel, you don't need to bind to value, ngModel does that for you.使用 ngModel 时,您不需要绑定到值,ngModel 会为您完成此操作。 Perhaps this combination is causing the trouble?也许这种组合造成了麻烦?

I believe forevereffort has the answer for your but I just wanted to point out some things related to formatting.我相信foreverefort 有你的答案,但我只是想指出一些与格式相关的事情。

  1. this. isn't necessary in template s <tr *ngFor="let item of familyInfo"> is okay.在模板中不需要<tr *ngFor="let item of familyInfo">是可以的。
  2. there is really a difference between value="{{item.name | titlecase}}" and [value]="item.name | titlecase" (you should use the latter.) value="{{item.name | titlecase}}"[value]="item.name | titlecase"之间确实有区别(您应该使用后者。)
  3. try to keep css in the .scss files尝试将 css 保留在.scss文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM