【问题标题】:angular form is not detecting the value changing角度形式未检测到值变化
【发布时间】:2021-06-04 23:51:39
【问题描述】:

我不知道为什么表单不起作用! 输入不会改变表单中的值。

valueChanges 没有检测到任何东西。 有什么帮助吗?

应用于堆栈闪电战: https://stackblitz.com/edit/angular-ivy-w68v69?file=src/app/app.component.ts

import { Component, VERSION } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";

@Component({
  selector: "my-app",
  template: `
    <form [formGroup]="form">
      <input
        FormControlName="patientName"
        placeholder="Enter Patient Name ..."
      />
    </form>
  `,
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  form: FormGroup;

  ngOnInit() {
    this.form = new FormGroup({
      patientName: new FormControl("")
    });

    this.form.get("patientName").valueChanges.subscribe(x => console.log(x));

    this.form.valueChanges.subscribe(next => {
      console.log("valueChanges didtted");
      console.log(next);
    });

    console.log("form created");
  }
}


【问题讨论】:

    标签: javascript angular typescript ionic-framework


    【解决方案1】:

    HTML 部分中的属性FormControlName 应该有小写的“F”:formControlName="patientName"

    如果你纠正这个,它会起作用:https://stackblitz.com/edit/angular-ivy-cjntrr?file=src/app/app.component.ts

    【讨论】:

    • 对。需要formControlName
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2021-01-27
    • 1970-01-01
    • 2021-08-07
    相关资源
    最近更新 更多