简体   繁体   English

角度和离子-为什么此指令在离子输入变化时未注册?

[英]Angular and Ionic - why this directive does not register on change on ion-input?

I'm trying make this directive work with ion input, however I'm having two issues I have no idea how to solve: 我正在尝试将此指令与离子输入一起使用,但是我遇到两个我不知道如何解决的问题:

1 - Can't load the mask when the ion-input is shown - The problem is that the input element only appears at the nativeElement's childNodes after some time, so I didn't found a hook to implement. 1-当显示离子输入时无法加载遮罩-问题是输入元素仅在一段时间后才出现在nativeElement的childNodes上,所以我没有找到实现钩子。 To try solve it I put some code inside a setTimeout, this way I can find the wrapped input: 为了解决这个问题,我将一些代码放在setTimeout中,这样我可以找到包装的输入:

public ngOnInit():void {
    setTimeout(()=>{
        this._element=this.findElement();
        resolvedPromise.then(() => this._applyValueChanges());
    },1000);
}
findElement():any{
    if (this._elementRef.nativeElement.tagName === 'INPUT') {
        return this._elementRef.nativeElement
    } else {
        let index;
        for(index=0;index<this._elementRef.nativeElement.childNodes.length;index++)
            if (this._elementRef.nativeElement.childNodes[index].tagName === 'INPUT')
                return this._elementRef.nativeElement.childNodes[index];
    }
}

2 - The other problem is that registerOnChange method is never called, this way I can't update the model to remove the special chars: 2-另一个问题是,永远不会调用registerOnChange方法,因此我无法更新模型以删除特殊字符:

public registerOnChange(fn:any):void {
    this._onChange = fn;
    return;
}

So, I would like to know: how can I make registerOnChange work with ion-input element? 因此,我想知道:如何使registerOnChange与离子输入元件一起工作? Is there a way I can find the wrapped input in onInit without using a setTimeout? 有没有一种方法可以在不使用setTimeout的情况下在onInit中找到包装的输入?

  1. Answer for first question is that you can use ionViewDidLoad() {} this function in place of your function. 第一个问题的答案是,您可以使用ionViewDidLoad(){}代替您的函数。
  2. On blur event is not part of latest ionic it has been removed so you can use this in place of that 在模糊事件中,它不是最新离子的一部分,因此已被删除,因此您可以用它代替

    it will work. 它会工作。

It all worked for me hope it work for you as well. 一切对我都有效,希望对您也一样。

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

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