gpt4 book ai didi

html - ngSubmit 不工作

转载 作者:太空狗 更新时间:2023-10-29 15:23:58 26 4
gpt4 key购买 nike

我有一个 angular 4 表单,我正在尝试提交数据

HTML 代码

<form class="form-horizontal"   [formGroup]="signupForm" 
(ngSubmit)="onFormSubmit()" >
<fieldset>
<legend>Scheduler</legend>
<!--- Gender Block -->
<div class="form-group">
<label for="scheduleJob">Schedule Job</label>
<input type="text" formControlName = "schedulejob"
id="scheduleJob"
placeholder="Schedule Job">

按钮代码

 <div class="form-group">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>

</div>

Scheduler.TS 文件

import { Component, OnInit } from '@angular/core';
import { Scheduler } from 'rxjs/Scheduler';
/* Import FormControl first */
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
selector: 'app-scheduler',
templateUrl: './scheduler.component.html',
styleUrls: ['./scheduler.component.css']
})
export class SchedulerComponent implements OnInit {

//Gender list for the select control element
private scheduleTypeList: string[];
//Property for the user
private scheduler:Scheduler;

private signupForm: FormGroup;

constructor(private fb:FormBuilder) { }

ngOnInit() {

this.scheduleTypeList = ['Type 1', 'Type 2', 'Type 3'];
this.signupForm = this.fb.group({
schedulejob: ['', Validators.required] ,
frequency: ['', Validators.required],
days: ['', Validators.required],
zone: ['', Validators.required],
schedulerjobtype:['', Validators.required]
})
}
public onFormSubmit() {
this.scheduler = this.signupForm.value;
if(this.signupForm.valid) {
this.scheduler = this.signupForm.value;
console.log(this.scheduler);
// alert(this.scheduler);
/* Any API call logic via services goes here */
}
//alert(this.scheduler);
console.log(this.scheduler);
}
}

为什么在提交点击和警报时没有将执行传递给 onFormSubmit 或 console.log 不打印值?

最佳答案

就像我在评论中所说的那样,如果您的按钮不在您的表单中,它将不会提交。

所以改为:

<form>
...
<button type="submit">Submit</button>
</form>

但是,如果您的操作方式有所不同,则可以将其放在室外,如所述 in this question .

关于html - ngSubmit 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50637044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com