使用formControl应用NgIF条件

时间:2019-09-12 08:41:58

标签: angular7 angular-ng-if

我想使用formcontrol而不是ngModel来比较我的值,当在输入框中输入值时,我想显示我的取消图像,所以我在subscribe中将userTextValue设置为true,现在的查询是如何重置单击取消时的值。我希望输入框为空,现在取消按钮已隐藏,但仍然有可用值,我正在使用管道过滤值。

<input matInput class="form-control input" placeholder="Enter name or Id" id="user"
    [formControl]="userControl"[matAutocomplete]="auto>
       <img src="assets/icons/cancel.svg" *ngIf="userTextvalue" class="cancel-icon" 
        aria-label="Clear" (click)="clearUserValues()">

ts:

  constructor() {
    this.userControl.valueChanges.subscribe((user) => {
      this.filterResult = user;
      this.userTextvalue = true;
    });
  }
  clearUserValues() { 
    this.filterResult = "";
    this.userTextvalue = false;
  }

pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'filterUser'
})
export class FilterUserPipe implements PipeTransform {

  transform(items: any[], searchText: string): any[] {
    if (searchText && searchText.length > 1) {
      items = searchText ? this.filterUsers(items, searchText) : items.slice();
    } else {
      items = [];
    }
    return items;
  }

  filterUsers(items: any[], value: string): any[] {
    const filterValue = value.toLowerCase();
    return items.filter(item => item.name.toLowerCase().indexOf(filterValue) === 0);
  }

}

0 个答案:

没有答案