如何从Anglar7的指令中的自定义输入组件获取值?

问题描述 投票:0回答:1

我有一个angular7应用,其中我有一个custom input组件,它在多个位置调用,例如component.html

component.ts

  handleChange(value) {
   this.payment.collectedAmount = value;
   this.calculateRemainingBalance();
  }

现在我有了指令,我将在其中格式化输入组件的值,但目前我只是在directive组件的custom内获取该值,像这样

    @Input() currencyValue: any;

    constructor(private el: ElementRef) {
    }

    ngAfterViewInit() {
      // this.el.nativeElement.focus();
      console.log('currency value : ',this.currencyValue)
    }

    @HostListener('focus', ['$event.target']) onfocus(){
       console.log('in focus');
    }

现在,当我运行应用程序并加载页面时,因此directive仅在页面加载时记录此行console.log('currency value : ',this.currencyValue)一次,然后在ngAfterViewInitHostListener中都不再记录。我如何从custom内部的directive输入组件中获取值?

angular typescript
1个回答
0
投票
AfterviewInit vill仅触发一次。它是生命周期的钩子。对于您的其他听众,只有集中精力,它才会起作用。因此,您可能希望按下按键或按下按键,按下按键或模糊按键来满足您的要求。

<input type="text" your-directive /> @Directive({ selector: 'yourSelector' }) export class TestDirective { @HostListener('keydown', ['$event']) onKeyDown(e) { console.log('in focus'); } }

© www.soinside.com 2019 - 2024. All rights reserved.