Angular2 - OnInit中的ngOnChanges值 - 指令

时间:2017-01-03 12:23:29

标签: angular lifecycle angular2-directives

我有一个掩码输入字段的指令。该指令为输入分配一个默认值,当我输入某个东西时,这个指令掩盖了值......它可以工作。

但是现在我想使用相同的组件来编辑值,所以我将值作为参数传递,但由于指令的OnInit方法,该值被覆盖。

指令:

ngOnInit(): void {
    //here my parameter value is being overwriten
    //I would like to know if it's possible to check the value of the input (which was passed as parameter) here (same value I have in the method below) so that I'll just execute next line if the value is empty/null
  if(check value of input if its possible)
    this.model.valueAccessor.writeValue("SO-");
}

onInputChange(value) {
      //here I have the value of parameter I'm passing...this is executed before ngOnInit.
    }
}

我的表格

this.myForm= this.formBuilder.group({
            serialNumber: [
                this.serialNumber, Validators.compose(
                [
                    Validators.required,
                    Validators.pattern('SO-([0-9]{5})\.([0-9]{2})-([0-9]{2})-([0-9]{3})'),
                    this.serialNumberExistsDirective.validate(this.registeredSerials)
                ]
            )],
            calibrationFile: [this.imageName, Validators.required]
        });

this.serialNumberthis.imageName是我在需要时设置的局部变量。 我有一个解决方法 - 只需在onInputChange中实现ngOnit的逻辑 - 但我想知道是否有可能实现这样的事情。

提前致谢。

0 个答案:

没有答案