Angular2 - ngOnChanges value in OnInit - Directives

551 views Asked by At

I have a directive that mask a input field. This directive assign a default value to the input, when I type something, this directive mask the value...it works.

But now I want to use the same component to edit the value, so I pass the value as parameter but this value is being overwrited because of the directive's OnInit method.

directive:

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.
    }
}

my form

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.serialNumber and this.imageName are local variable that I set when I need. I have a workaround - just implement the logic of ngOnit inside onInputChange - but I was wondering if it's possible to achieve such a thing.

Thanks in advance.

0

There are 0 answers