我正在尝试从表单中获取值,并且出现此错误:
错误TypeError:无法读取未定义的属性'get'

 addMessage: FormGroup;
 get message(){ return this.addMessage.get('message').value}

HTML:
 <form [formGroup]="addMessage" (ngSubmit)="sendMessage()">
      <input formControlName="message" class="input" type="text">
      <button type="submit" class="button is-primary">Send</button>
  </form>

最佳答案

您所做的一切都是对的,但是您也需要构建一个FormBuilder。

请遵循我从表单获取数据的方法。

的HTML:

<form [formGroup]="WordpressForm">

 <div class="form-group">
   <mat-form-field class="example-full-width">
     <input matInput placeholder="Title" [(ngModel)]='title' formControlName='title'>
   </mat-form-field>
   <div>
   <button (click)="save()">Save</button>
  </div>
 </div>
</form>

ts:
constructor(private fb: FormBuilder) {}

WordpressForm = this.fb.group({
  title: ['', [Validators.required]]
});

getTitle() {
 return this.WordpressForm.get('title');
}

save() {
 const template_Title = this.title;
 console.log(template_Title);
}

关于angular - FormGroup错误TypeError : Cannot read property 'get' of undefined,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49980470/

10-12 00:17
查看更多