我不知道为什么表格不起作用! 输入不会改变表单中的值。
并且 valueChanges
没有检测到任何东西。 有什么帮助吗?
应用于stackblitz: 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");
}
}
HTML 部分中的属性 FormControlName
应该有小写的“F”:formControlName="patientName"
。
如果你纠正了这个,它就有效:https://stackblitz.com/edit/angular-ivy-cjntrr?file=src/app/app.component.ts