【问题标题】:Angular 5: ngModel binding not workingAngular 5:ngModel 绑定不起作用
【发布时间】:2018-08-03 16:11:27
【问题描述】:

我很确定我正确使用了ngModel,但是当文本字段更改时,我的模型不会更新。我在我的应用程序中多次使用这个星座,没有任何问题。我已经坐在上面一段时间了,没有看到我的错误,也许第二双眼睛会有所帮助。难道ngIf会影响绑定?

我正在使用 Angular5。还有Maseemann Components


更新:

我编辑了第一个用于测试的文本字段,如下所示:

<p>
  <mdl-textfield type="text" label="Name" [(ngModel)]="assignment.name" (ngModelChange)="log()" floating-label></mdl-textfield>
  {{assignment.name}}
</p>

它按预期工作,直接显示输入。我检查了几次拼写,但没有发现任何错误。


我的模板:

<mdl-card mdl-shadow="2" *ngIf="state === 0">
  <mdl-card-title>
    <h2 mdl-card-title-text>Assignment erstellen</h2>
  </mdl-card-title>
  <mdl-card-supporting-text>
    <div class="mdl-grid">
      <div class="mdl-cell mdl-cell--7-col">
        <p>
          <mdl-textfield type="text" label="Name" [(ngModel)]="assignment.name" (ngModelChange)="log()" floating-label></mdl-textfield>
        </p>
        <p>
          <mdl-select [(ngModel)]="assignment.lang" placeholder="Programmiersprache auswählen">
            <mdl-option [value]="'c'">C / C++</mdl-option>
            <mdl-option [value]="'java'">Java</mdl-option>
            <mdl-option [value]="'py'">Python</mdl-option>
            <mdl-option [value]="'m'">Matlab</mdl-option>
          </mdl-select>
        </p>
        <p>
          <mdl-textfield type="number" label="Punkte" [(ngModel)]="assignment.points_recommended" floating-label></mdl-textfield>
        </p>
        <p>
          <mdl-textfield type="text" label="Executionfilename" [(ngModel)]="assignment.execution_filename" floating-label></mdl-textfield>
        </p>
        <p>
          <mdl-textfield type="text" label="Outputfilenames" [(ngModel)]="assignment.output_files" floating-label></mdl-textfield>
        </p>
        <div class="mdl-file_upload">
          <h6>Evaluationfile:</h6>
          <label for="file-upload" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored">
            <span>Datei auswählen</span>
          </label>
          <mdl-textfield type="text" [(ngModel)]="assignmentFile.name" floating-label disabled></mdl-textfield>
          <input type="file" id="file-upload" (change)="onFileSelect($event.target.files)">
        </div>
        <p>
          <mdl-switch [(ngModel)]="assignment.visible_for_others" mdl-ripple>Öffentlich</mdl-switch>
        </p>
        <p>
          <mdl-switch [(ngModel)]="assignment.text_editor_available" mdl-ripple>Online-Texteditor</mdl-switch>
        </p>
      </div>
      <div class="mdl-cell mdl-cell--5-col">
        <h6>Beschreibung</h6>
        <p>...</p>
      </div>
    </div>
  </mdl-card-supporting-text>
  <mdl-card-actions mdl-card-border>
    <button (click)="submitAssignment()" mdl-button mdl-colored mdl-ripple mdl-colored="primary">Nächster Schritt</button>
  </mdl-card-actions>
</mdl-card>

我的控制器:

export class AssignmentNewComponent implements OnInit {
  public state: number = 0;
  public assignment: Assignment = new Assignment(null, this.authService.getAccountId());
  public assignmentFile: File = new File([''], 'Datei auswählen');

  constructor(
    private authService: AuthService,
    private assignmentService: AssignmentService,
  ) { }

  ngOnInit() {
    this.testcases.push(new AssignmentTestCase());
  }

  onFileSelect(files: FileList): void {
    this.assignmentFile = files[0];
  }

  setState(state: number): void {
    this.state = state;
  }

  submitAssignment(): void {
    Object.keys(this.assignment).forEach(
      key => console.log(key, this.assignment[key])
    )
  }


  next(): void {
    this.state++;
  }

  previous(): void {
    this.state--;
  }

  log(){
    console.log(this.assignment);
  }

}

我的模特:

export class Assignment {
    public id: number;
    public admin: number;
    public name: string;
    public lang: string;
    public points_recommended: number;
    public execution_filename: string;
    public output_files: string;
    public evaluation_file: string;
    public visible_for_others: boolean;
    public text_editor_available: boolean;
    public created_at: string;
    public updated_at: string;

    constructor(
        id?: number,
        admin?: number,
        name?: string,
        lang?: string,
        points_recommended?: number,
        execution_filename?: string,
        output_files?: string,
        evaluation_file?: string,
        visible_for_others?: boolean,
        text_editor_available?: boolean,
        created_at?: string,
        updated_at?: string,
    ){
        this.id = id || undefined;
        this.admin = admin || undefined;
        this.name = name || undefined;
        this.lang = lang || undefined;
        this.points_recommended = points_recommended || undefined;
        this.execution_filename = execution_filename || undefined;
        this.output_files = output_files || undefined;
        this.evaluation_file = evaluation_file || undefined;
        this.visible_for_others = visible_for_others || undefined;
        this.text_editor_available = text_editor_available || undefined;
        this.created_at = created_at || undefined;
        this.updated_at = updated_at || undefined;
    }
}

【问题讨论】:

  • 你能不能试着把构造函数代码移到 ngOnInit();公共赋值:Assignment = new Assignment(null, this.authService.getAccountId());
  • 我试过了。但仍然是相同的行为。
  • 代替未定义,你可以设置''(空值)。为什么我们需要 *ngIf?。
    标签
    也缺少 ngForm 指令
  • 为什么我需要 ngForm?但我会添加它。并且ngIf是需要的,因为您有一些状态,首先您将创建一个作业,而不是上传其他文件。这应该是用户的某种指南,所以我不会在他点击“Nächster Schritt”(下一步)后显示作业。
  • ngIf 没有影响

标签: javascript angular typescript angular5


【解决方案1】:

这个问题很容易解决。我有一个角度 ID &lt;span #assignment&gt;..,它将我的赋值变量新绑定到这个赋值。只是命名问题。我重命名了 id 现在它按预期工作。

【讨论】:

    猜你喜欢
    • 2017-10-20
    • 1970-01-01
    • 2021-12-04
    • 2015-10-15
    相关资源
    最近更新 更多