角度自定义指令在同一视图上多次使用,行为不正确

我有一个自定义指令来修改我的输入数据...我在指令中有一个输入事件侦听器和一个onChange方法。 问题是,当我在视图中将它与单个元素一起使用时,它可以完美地工作。 但是,当我在不同输入的同一视图上多次使用它时,输入事件侦听器可以工作,但是onChange方法仅在更改带有指令的最后一个输入字段时才运行,并且我无法弄清楚为什么...我的指令


@Directive({
  selector: '[testDirective]',providers: [DecimalPipe]
})
export class TestDirective implements OnChanges,OnInit {
  ngOnInit(): void {
    console.log('DIR->',this.inputvalue);
  }

  element: ElementRef;
  prevValue = 0;
  cd: ChangeDetectorRef;
  decimalPipe: DecimalPipe;
  constructor(
    Element: ElementRef,decimalPipe: DecimalPipe,cd: ChangeDetectorRef
  ) {
    this.element = Element;
    this.decimalPipe = decimalPipe;
    this.cd = cd;
  }



  @HostListener('input',['$event'])
  inputChanged(event) {

     console.log(event.target.value);
  }
  //array: any = [];
  ngOnChanges(changes: SimpleChanges) {
    console.log(changes,'Changes');

    let changed = changes.inputvalue;
    //this.array.push(changed);
    console.log(changed);
    if (changed) {
      console.log(changed,changed.previousValue);
    }
  }


  @Input() public number: any;
  @Input() public precision: any;
  @Input() public inputvalue: any;

}

我的HTML

<input id="input1"
type="text" #value
testDirective [precision]="2" [inputvalue]="value.value"
formControlName="input1"/>
<input  id="input2"
type="text" #value
testDirective [precision]="2" [inputvalue]="value.value"
formControlName="input2"/>
johnethan 回答:角度自定义指令在同一视图上多次使用,行为不正确

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2914759.html

大家都在问