填写表格时,您能以编程方式提交角度表单吗?



我想在填写表单时以编程方式提交表格?

TS:

@ViewChild('f', { static: false }) public form: NgForm;
constructor(){}
public ngOnChanges(changes: any): void {}
public ngOnInit(): void {}

模板:

<form class="form" #form="ngForm" (ngSubmit)="onSubmit()">
<div>
<label for="name">Name</label>
<input type="text" [(ngModel)]="myObj.name" name="name" placeholder="Your Name" required>
</div>
</form>

我认为"反应式表单"是适合您的关键字。

您可以使用".subscribe(("定义规则并向表单添加订阅

myForm = new FormGroup({
name: new FormControl(''),
});
constructor() {
this.myForm.get('name').valueChanges.subscribe(x => {
console.log('name value changed', x);
// some magic
this.onSubmit(); // call submit
});
}

最新更新