Pass parameter to Angular 4 directive on input

前端 未结 3 898
我寻月下人不归
我寻月下人不归 2021-02-19 19:49

I have an input text field like this


and my dire

相关标签:
3条回答
  • 2021-02-19 20:23

    Try this.

    Update:

    import {Directive, SimpleChanges} from '@angular/core';
    
    @Directive({
      selector: '[inputTextFilter]'
    })
    export class MyDirective {
      @Input('inputTextFilter') params: string;
      constructor(){}
      ngOnInit(){
         console.log(this.params)
      }
    }
    
    0 讨论(0)
  • 2021-02-19 20:43

    Try like this in directive :

    import {Directive, Input, ElementRef} from 'angular2/core';
    @Directive({
        selector: '[inputTextFilter]'
    })
    class FocusDirective {
        @Input() inputTextFilter: any;
        protected ngOnChanges() {
             console.log('inputTextFilter', this.inputTextFilter);
        }
    }
    
    0 讨论(0)
  • 2021-02-19 20:44

    In the hope that this helps someone else...the problem is in the template.

    When I pass the input as [myDirective]="A" the A is being intpreted as an undefined variable. Since I wanted to pass the letter A I should have said [myDirective]="'A'"

    0 讨论(0)
提交回复
热议问题