Angular fields not retaining disabled on switching tab

Discussão em 'Angular' iniciado por Sha Na, Fevereiro 20, 2025.

  1. Sha Na

    Sha Na Guest

    I have angular modal which is made of several tabs. On a single tab, when unchecking on a checkbox, some other fields are disabled. However, when I switch tab again, the fields are becoming enabled. I have used json form config to dynamically build my html so cant use viewchild.

    My html tab is ngbNav. Help pls :(

    ngAfterViewInit(): void {
    this.dishCategories = this.menuService.getDishCategoryFromToken();

    if (!Array.isArray(this.dishCategories)) {
    this.dishCategories = this.dishCategories ? [this.dishCategories] : [];
    }

    if (this.dishCategories.includes('SpicySpecials')) {
    this.prepareSauce();
    this.disableSides();
    this.toggleSpicyLevel();
    this.cdr.detectChanges();
    }
    }

    private disableSides() {
    const isExtraSpicy = this.dynamicOrderForm.get('extraSpicy');
    const spicyLevelInput = this.dynamicOrderForm.get('spicyLevel');
    const sideDishesAcc = this.dynamicOrderForm.get('sideDishes');
    const drinkSelection = this.dynamicOrderForm.get('drinkSelection');
    const dessertOption = this.dynamicOrderForm.get('dessertOption');

    if (isExtraSpicy?.value == false) {
    drinkSelection?.enable();
    spicyLevelInput?.disable();
    sideDishesAcc?.disable();

    if (drinkSelection?.value == false)
    {
    dessertOption?.disable();
    }
    } else {
    drinkSelection?.reset(false);
    drinkSelection?.disable();
    dessertOption?.disable();
    }

    if (isExtraSpicy) {
    isExtraSpicy.valueChanges.subscribe((newValue) => {
    if (newValue === true) { // extra spicy selected
    spicyLevelInput?.enable();
    drinkSelection?.patchValue(false);
    drinkSelection?.disable();

    const dessertElement = document.querySelector(`.dessertSelect select`) as HTMLSelectElement;
    dessertOption?.patchValue(null);
    dessertElement.selectedIndex = 0;
    dessertElement.disabled = true;

    sideDishesAcc?.enable();
    } else { // no extra spice
    const spicyLevelElement = document.querySelector(`.spicyLevel input`) as HTMLInputElement;
    console.log('element:', spicyLevelElement

    Continue reading...

Compartilhe esta Página