Home > Net >  Call without arguments
Call without arguments

Time:11-25

just found a code snippet like this:

  validate(formControl: FormControl){
    const isValid = true;
    return isValid ? null : {
      emailValid: {
        valid: true
      }
    };   }

  validateData() {
    return  [
      {value: this.validate, errorMsg: "error" }
    ];   }

I didn't understand what is (conceptually) the call this.validate after value:, it's called without the () and is correctly executed. If i put a console.log(formControl.value) in validate I get the correct value data.

If I use value: this.validate() I obviously get the error about the missing argument.

What I'd like to do is pass another argument, ie: validate(formControl: FormControl, user: string){} and i get the missing argument error.

There's a way to get the formControl like in the value: this.validate and at the same time pass the argument?

Already tried to store the data in a global var but the item is initialized before population.

Thanks

CodePudding user response:

This is not about Angular, it's Javascript.

What you pass in the "value: this.validate" called reference.
Let say you have this function

function callMe(callback){
  callback(123);
}

And when you call it, you simply do: callMe(console.log).

Similar to your example:

function validate(formControl){}

function ValidateData(validateFn){
  validateFn(formControl)
}
  • Related