ng提交无效

时间:2018-06-01 06:09:11

标签: html angular angularjs-directive angular4-forms

我有一个角度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);
}
}

为什么在提交点击和提醒或者console.log没有打印值时,执行没有传递给onFormSubmit?

5 个答案:

答案 0 :(得分:14)

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

所以改为:

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

如果你做的有点不同,可能会把它放在外面,如in this question所述。

答案 1 :(得分:1)

您需要将按钮代码放在表单中,如下所示

<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">

  <!-- Button Code Here -->

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

  </div>
   <!--  Form ends here -->
 </form>

答案 2 :(得分:1)

我遇到了同样的问题,因为我没有使用<form>标签来包装表单。

答案 3 :(得分:0)

您还可以在提交按钮中使用表单属性。此属性使用表单的 id 来引用它:

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

答案 4 :(得分:-1)

您可以做的一个小技巧是在表单中放置一个辅助隐藏按钮,然后让主按钮以编程方式单击该辅助按钮:

<form [formGroup]="form" (ngSubmit)="submit()">
  ...
  <button type="submit" hidden #btn></button>
</form>
...
<button (click)="btn.click()">Submit</button>