typescript 使用FormBuilder和AngularMaterial的Angular - Reactive Form

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 使用FormBuilder和AngularMaterial的Angular - Reactive Form相关的知识,希望对你有一定的参考价值。

import { Component, Output } from '@angular/core';
import { FormControl, Validators, FormGroup, FormBuilder } from '@angular/forms';
import { EventEmitter } from 'events';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  @Output() formSubmitEvent: EventEmitter = new EventEmitter();
  @Output() formCancelEvent: EventEmitter = new EventEmitter();

  public allTags: any[] = [1, 2, 3, 4, 5, 6];
  public bookForm: FormGroup;

  constructor(private fb: FormBuilder) {
    this.bookForm = this.fb.group({
      title: ['', Validators.required],
      author: ['', Validators.required],
      tags: [[]]
    });
  }

  submit(formData) {
    return this.formCancelEvent.emit(formData);
  }

  cancel() {
    return this.formCancelEvent.emit(null);
  }
}


// <form [formGroup]="bookForm">
//   <mat-form-field floatLabel="Title">
//     <mat-label>Title</mat-label>
//     <input matInput formControlName="title" placeholder="Title" required>
//   </mat-form-field>

//   <mat-form-field floatLabel="Author">
//     <mat-label>Author</mat-label>
//     <input matInput formControlName="author" placeholder="Author" required>
//   </mat-form-field>

//   <mat-form-field>
//     <mat-select placeholder="Tags" formControlName="tags" multiple>
//       <mat-select-trigger>
//         {{bookForm.controls['tags'].value ? bookForm.controls['tags'].value[0] : ''}}
//         <span *ngIf="bookForm.controls['tags'].value?.length > 1" class="example-additional-selection">
//           (+ {{bookForm.controls['tags'].value.length - 1}} others)
//         </span>
//       </mat-select-trigger>
//       <mat-option *ngFor="let tag of allTags" [value]="tag">{{tag}}</mat-option>
//     </mat-select>
//   </mat-form-field>

//   <div class="form-footer">
//     <button color="primary" type="submit" (click)="submit(bookForm.value)" mat-raised-button>Save</button>
//     <button type="button" mat-raised-button (click)="cancel()">Cancel</button>
//   </div>
// </form>

以上是关于typescript 使用FormBuilder和AngularMaterial的Angular - Reactive Form的主要内容,如果未能解决你的问题,请参考以下文章

使用 jQuery formbuilder 渲染 JSON 表单

没有 Bootstrap 库的 Angular Formbuilder Select 和 Dropdown

使用 formbuilder 在组合框中选择默认值

html 响应式表单:使用FormBuilder

有啥办法可以防止 FormBuilder 元素滚动到页面顶部?

使用 Symfony2 formbuilder 显示缩略图而不是 img 的名称