最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 2 : ERROR TypeError: Cannot read property 'value' of undefined - Stack Overflow

programmeradmin5浏览0评论

I am getting the following error on browser console after clicking on Submit button.

In this application I am trying to get information about the Student uploaded code below. I am unable to find why this error is shown on console. I have correctly added the formControlName.

  1. Component

            import { Component, OnInit, Inject } from '@angular/core';
            import { FormGroup, FormControl, Validators, FormBuilder, AbstractControl } from '@angular/forms';
    
            @Component({
              selector: 'app-new-record',
              templateUrl: './new-recordponent.html',
              styleUrls: ['./new-recordponent.css']
            })
            export class NewRecordComponent implements OnInit {
              myFormGroup: FormGroup;
              constructor(private formBuilder: FormBuilder) {
                this.myFormGroup = this.formBuilder.group({
                  name: new FormControl('', Validatorspose([
                    Validators.required
                  ])),
                  claz: new FormControl('BCA'),
                  admissionYear: new FormControl(Validatorspose([Validators.required]))
                });
              }
    
              ngOnInit() {
    
              }
    
              onSubmit(student) {
                console.log('onSubmit called !');
              }
    
            }
    
    1. Template

          <form [formGroup]="myFormGroup"
          (ngSubmit)="onSubmit(form.value)">
              <div class="form-group">
                  <label for="claz">Class:</label> <select name="claz" formControlName="claz">
                      <option value="MCA">MCA</option>
                      <option value="BCA">BCA</option>
                      <option value="M.Sc">M.Sc</option>
                      <option value="B.Tech">B.Tech</option>
                  </select>
              </div>
              <div class="form-group">
                  <label for="name">Name:</label> <input type="text"
                      class="form-control" id="name" formControlName="name">
              </div>
              <div class="form-group">
                  <label for="admissionYear">Admission Year:</label> <input type="number"
                      class="form-control" id="admissionYear" formControlName="admissionYear">
              </div>
              <button type="submit" class="btn btn-default" >Submit</button>
          </form>
      

I am getting the following error on browser console after clicking on Submit button.

In this application I am trying to get information about the Student uploaded code below. I am unable to find why this error is shown on console. I have correctly added the formControlName.

  1. Component

            import { Component, OnInit, Inject } from '@angular/core';
            import { FormGroup, FormControl, Validators, FormBuilder, AbstractControl } from '@angular/forms';
    
            @Component({
              selector: 'app-new-record',
              templateUrl: './new-record.ponent.html',
              styleUrls: ['./new-record.ponent.css']
            })
            export class NewRecordComponent implements OnInit {
              myFormGroup: FormGroup;
              constructor(private formBuilder: FormBuilder) {
                this.myFormGroup = this.formBuilder.group({
                  name: new FormControl('', Validators.pose([
                    Validators.required
                  ])),
                  claz: new FormControl('BCA'),
                  admissionYear: new FormControl(Validators.pose([Validators.required]))
                });
              }
    
              ngOnInit() {
    
              }
    
              onSubmit(student) {
                console.log('onSubmit called !');
              }
    
            }
    
    1. Template

          <form [formGroup]="myFormGroup"
          (ngSubmit)="onSubmit(form.value)">
              <div class="form-group">
                  <label for="claz">Class:</label> <select name="claz" formControlName="claz">
                      <option value="MCA">MCA</option>
                      <option value="BCA">BCA</option>
                      <option value="M.Sc">M.Sc</option>
                      <option value="B.Tech">B.Tech</option>
                  </select>
              </div>
              <div class="form-group">
                  <label for="name">Name:</label> <input type="text"
                      class="form-control" id="name" formControlName="name">
              </div>
              <div class="form-group">
                  <label for="admissionYear">Admission Year:</label> <input type="number"
                      class="form-control" id="admissionYear" formControlName="admissionYear">
              </div>
              <button type="submit" class="btn btn-default" >Submit</button>
          </form>
      
Share Improve this question edited May 14, 2017 at 8:28 Trash Can 6,8245 gold badges27 silver badges39 bronze badges asked May 14, 2017 at 8:25 Tushar GiraseTushar Girase 3637 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

There is no form defined, instead use myFormGroup which has been defined as formGroup

(ngSubmit)="onSubmit(myFormGroup.value)"
发布评论

评论列表(0)

  1. 暂无评论