简体   繁体   English

我需要创建一个 Angular 指令来清除图标按钮单击输入字段中输入的内容

[英]I need to create a Angular directive to clear content entered in input field in icon button click

I have created a directive which will show clear button icon only if there is any content inside input field.我创建了一个指令,仅当输入字段中有任何内容时才会显示清除按钮图标。 Also I need that on button click input content should get cleared.我还需要在按钮单击时输入内容应该被清除。

As host element is not input itself value is setting to empty inside directive but not getting reflected in the component.由于主机元素本身未输入,因此值在指令内部设置为空,但未反映在组件中。

HTML HTML

<mat-form-field>
    <input matInput  #inputVal type="text" placeholder="Clearable input" [(ngModel)]="value" />
    <mat-icon [clearInput]= "inputVal.value" class="suffix" matSuffix >Close</mat-icon>
</mat-form-field>

Directive指示

@Directive({
  selector: '[clearInput]',
  exportAs: 'clearInput'
})
export class clearInputDirective implements OnChanges{
   
    @Input('clearInput') inputValue: any;
    constructor(private el: ElementRef, private renderer:Renderer2) {
      
  }
  @HostListener('click') onClick() {
    this.inputValue = null;
  }

  ngOnChanges(changes: SimpleChanges){
    if(changes.inputValue){
   
     if(this.inputValue){
        this.renderer.setStyle(this.el.nativeElement, 'display', 'block');
       }
       else {
        this.renderer.setStyle(this.el.nativeElement, 'display', 'none');
       }
    }
  }
}

This problem acc.这个问题符合。 to me is clearInput is not bind to your inputVal it's just taking a value and acting accordingly in directive.对我来说clearInput没有绑定到您的inputVal它只是取一个值并在指令中相应地采取行动。 for your approach to work you need full reference of inputVal in directive.对于您的工作方法,您需要在指令中完整引用inputVal

Right now it's just a primitive value that's why when you change the value to null it's only on directive level.现在它只是一个原始值,这就是为什么当您将值更改为 null 时,它只是在指令级别。

[EDIT] [编辑]

Try below code.试试下面的代码。 - as you are using ngmodel then we need reference of ngmodel - 当您使用 ngmodel 时,我们需要参考 ngmodel

<div class="input">
 <input matInput  clearInput [clearInput]= "value" #inputVal type="text" placeholder="Clearable input" [(ngModel)]="value" />
<div class="alwaysCloseButtonWillBethere" 
    matSuffix >Close</div>
</div>

clear directive -- just i have taken a constant like html is like div[input, close] when you click on close input will get empty clear 指令——只是我采用了一个常数,比如 html 就像div[input, close]当你点击关闭时输入会变空

    import { Directive, OnChanges, Input, HostListener, ElementRef, Renderer2, SimpleChanges, OnInit, OnDestroy} from '@angular/core';
import { NgModel } from '@angular/forms';

@Directive({
  selector: '[clearInput]',
  exportAs: 'clearInput'
})
export class ClaerinputDirective implements OnChanges , OnInit , OnDestroy{
   
    @Input('clearInput') inputValue: any;
    constructor(private el: ElementRef, private renderer:Renderer2 , private model: NgModel) {
      
  }

  ngOnInit(){
     this.el.nativeElement.parentNode.children[1].addEventListener('click', () =>{
       this.model.control.reset();
     })
  }

  ngOnChanges(changes: SimpleChanges){
   if(changes.inputValue){
      if(this.inputValue){
       this.renderer.setStyle(this.el.nativeElement.parentNode.children[1], 'display', 'block');
       }
       else {
        this.renderer.setStyle(this.el.nativeElement.parentNode.children[1], 'display', 'none');
       }
   }
  }

  ngOnDestroy() {
     this.el.nativeElement.parentNode.children[1].removeEventListener('click',null
     );
  }
}

if dom node are not static, then use below it based on class name i have taken class name --- alwaysCloseButtonWillBethere如果 dom 节点不是 static,则根据 class 名称在其下方使用我已采用 class 名称 --- alwaysCloseButtonWillBethere

     import { Directive, OnChanges, Input, HostListener, ElementRef, Renderer2, SimpleChanges, OnInit, OnDestroy} from '@angular/core';
import { NgModel } from '@angular/forms';

@Directive({
  selector: '[clearInput]',
  exportAs: 'clearInput'
})
export class ClaerinputDirective implements OnChanges , OnInit , OnDestroy{
   
    @Input('clearInput') inputValue: any;
    nodeToHideAndShow: HTMLElement;
    constructor(private el: ElementRef, private renderer:Renderer2 , private model: NgModel) {
      
  }

  getNode(){
   if(!this.nodeToHideAndShow){
     this.nodeToHideAndShow = this.el.nativeElement.parentNode.querySelector('.alwaysCloseButtonWillBethere');
    }

  }
  ngOnInit(){
    this.getNode();
    if(!this.nodeToHideAndShow){
      // alert('Add a class  alwaysCloseButtonWillBethere')
    } else {
      console.log('d');
      this.nodeToHideAndShow.addEventListener('click', () => {
       this.model.control.reset();
     })
    }
     
  }

  ngOnChanges(changes: SimpleChanges){
   if(changes.inputValue){
     this.getNode();
      if(this.inputValue){
       this.renderer.setStyle( this.nodeToHideAndShow, 'display', 'block');
       }
       else {
        this.renderer.setStyle( this.nodeToHideAndShow, 'display', 'none');
       }
   }
  }

  ngOnDestroy() {
    if( this.nodeToHideAndShow){
          this.nodeToHideAndShow.removeEventListener('click',null);
      }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM