Home > Net >  how i can apply an if confition on a field of formBuilder.group Angular
how i can apply an if confition on a field of formBuilder.group Angular

Time:11-25

Im trying to use if condition instead of ? : for the filed booleanInput using Angular but i don't khow how.

  ngOnInit(): void {
        this.parameterForm = this.formBuilder.group({
            booleanInput:
                this.ppf.type === 'boolean'
                    ? this.finalValue=== undefined
                        ? (this.finalValue= false)
                        : this.finalValue
                    : false
        });
    }

I did several searches but found nothing. Can someone help me please ?

CodePudding user response:

As far as I understand, You wanna assign this control a value i.e. false or true. You can do this by the following improvements. Might be helpful. Thanks

 ngOnInit(): void {
 let finalValue;
 if(this.ppf.type === 'boolean') {
  if(finalValue === undefined) {
    finalValue = false
  } else {
    finalValue = this.finalValue
  }
 } else {
  finalValue = false;
 }
        this.parameterForm = this.formBuilder.group({
            booleanInput: finalValue
        });
    }

  • Related