1

I have a formly form with toggle field, I would like to detect when the user clicks on the toggle to disable it. here is what I have done so far:

        fields: {
          key: myToggle,
          id: myToggle,
          templateOptions:{
             label: myToggle
          }
          hooks:{
            onInit: (field) =>{
            field.formControl?.valueChanges.pipe(
              filter(e=>
                 e['property'] === 'model.myToggle' && e['type'] === 'expressionChanges'
                  ),
              tap(performAction)
             )
          }
        }
    }

I also tried this solution in the ngOnInit() method:

    this.form.valueChanges.pipe(
       takeUntilDestroyed(this.destroyRef)
       ).subscribe(() =>{
         perFormMyActionWhenFalseValueIsDetected();
    }

With the second solution, the method is called as times as the form field number. If I have 3 fields, the method will be called 3 times. How can you do specifically detect the event toggle field is set to false?

1 Answer 1

0

You have to get that particular control then listen for valueChanges.

You can also add a debounceTime to reduce the number of events.

this.form?.get('<form control name>')?.valueChanges.pipe(
   debounceTime(500),
   takeUntilDestroyed(this.destroyRef)
).subscribe(() => {
  perFormMyActionWhenFalseValueIsDetected();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.