需要角材料切屑

时间:2020-02-16 18:36:13

标签: angular validation angular-material

我刚接触过角形,还没有正确了解反应式。 我有一个基于Angular Material网站上的示例创建的组件:

https://material.angular.io/components/chips/overview

该组件的html如下所示:

<mat-form-field class="full-width-field">
    <mat-chip-list #chipList
        aria-label="Tags">
        <mat-chip *ngFor="let tag of tags"
            [selectable]="selectable"
            [removable]="removable"
            (removed)="remove(tag)">
            {{tag}}
            <mat-icon matChipRemove
                *ngIf="removable">cancel</mat-icon>
        </mat-chip>
        <input placeholder="Tags"
            #tagInput
            [formControl]="tagCtrl"
            [matAutocomplete]="auto"
            [matChipInputFor]="chipList"
            [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
            (matChipInputTokenEnd)="add($event)"
            required>
    </mat-chip-list>
    <mat-autocomplete #auto="matAutocomplete"
        (optionSelected)="selected($event)">
        <mat-option *ngFor="let tag of filteredTags | async"
            [value]="tag">
            {{tag}}
        </mat-option>
    </mat-autocomplete>
</mat-form-field>

该组件如下所示:

import { Component, OnInit, ViewChild, ElementRef } from "@angular/core";
import {
  MatAutocomplete,
  MatChipInputEvent,
  MatAutocompleteSelectedEvent
} from "@angular/material";
import { FormControl } from "@angular/forms";
import { Observable } from "rxjs";
import { COMMA, ENTER } from "@angular/cdk/keycodes";
import { startWith, map } from "rxjs/operators";

import { TagService } from "@services";

@Component({
  selector: "app-tags",
  templateUrl: "./tags.component.html",
  styleUrls: ["./tags.component.scss"]
})
export class TagsComponent implements OnInit {
  @ViewChild("tagInput", { static: false }) tagInput: ElementRef<
    HTMLInputElement
  >;
  @ViewChild("auto", { static: false }) matAutocomplete: MatAutocomplete;

  visible = true;
  selectable = true;
  removable = true;
  separatorKeysCodes: number[] = [ENTER, COMMA];
  tagCtrl = new FormControl();
  filteredTags: Observable<string[]>;
  tags: string[] = [];
  allTags: string[] = [];

  constructor(private tagService: TagService) {
    this.filteredTags = this.tagCtrl.valueChanges.pipe(
      startWith(null),
      map((tag: string | null) =>
        tag ? this._filter(tag) : this.allTags.slice()
      )
    );
  }

  ngOnInit() {
    this.tagService
      .list()
      .subscribe(tags => (this.allTags = tags.map(m => m.id as string)));
  }

  add(event: MatChipInputEvent): void {
    const input = event.input;
    const value = event.value;

    // Add our tag
    if ((value || "").trim()) {
      this.tags.push(value.trim());
    }

    // Reset the input value
    if (input) {
      input.value = "";
    }

    this.tagCtrl.setValue(null);
  }

  remove(tag: string): void {
    const index = this.tags.indexOf(tag);

    if (index >= 0) {
      this.tags.splice(index, 1);
    }
  }

  selected(event: MatAutocompleteSelectedEvent): void {
    this.tags.push(event.option.viewValue);
    this.tagInput.nativeElement.value = "";
    this.tagCtrl.setValue(null);
  }

  private _filter(value: string): string[] {
    const filterValue = value.toLowerCase();

    return this.allTags.filter(
      tag => tag.toLowerCase().indexOf(filterValue) === 0
    );
  }
}

我想添加 mat-error ,所以我尝试了以下操作:

<mat-error *ngIf="tagCtrl.errors?.required && (tagCtrl.touched && tagCtrl.dirty)">This is an error</mat-error>

但是没有用。 通常,在使用反应式表单时,我使用[formControlName],但是由于这是共享组件,因此它不属于表单,所以我猜这就是为什么他们使用[formControl]的原因??? 有人可以帮我吗?

0 个答案:

没有答案
相关问题