3

I am trying to disable button. But button is angular component. And Html5 disabled attribute cannot work on component selector.

I tried to add like this but does not work: [attr.disabled]="isOpenModal

Button Html code:

 <add-new-button [attr.disabled]="isOpenModal" 
                 (click)="openModal('new')"
                 class="nano-bc-green hover-effect">
 </add-new-button>

Button - Component "add new button"

   @Component({
    selector: 'nano-add-new-button',
    template: `
              <div class='nano-f-r nano-f'>
                    <i class='fa fa-plus'></i>
                    <span class='nano-ml-5'>
                        Add New
                    </span>
              </div>`
})
export class NanoAddNewButtonComponent {
}

Open Modal method which is used on button:

public openModal(id: string): void {
        const data = {id: id};
        this.modalModel.add(AudienceModel.ENTITY_NAME, data);
}

Any idea for solution?

3
  • may be you can add a class to change opacity and with pointer-events: none ? Commented May 10, 2018 at 10:05
  • have you tried this? docs.angularjs.org/api/ng/directive/ngDisabled Commented May 10, 2018 at 10:08
  • You can add an @Input in your component and bind it to the button you want to disable. Commented May 10, 2018 at 10:10

4 Answers 4

4

Because your add-new-button component can be anything, and disabled is not a property that all elements have, that can't work. Check out the list of Global Attributes.

You have to define your own disabled property:

@Input() disabled: boolean;

And you can bind this to the elements you want to disable like:

<button [disabled]="disabled">My button</button>

You can use it like this after:

 <add-new-button [disabled]="isOpenModal"
             (click)="openModal('new')"
             class="nano-bc-green hover-effect">
 </add-new-button>
Sign up to request clarification or add additional context in comments.

Comments

1

Just put the disabled logic into the click method itself:

Template:

 <add-new-button (click)="onModalClick()"
                 class="nano-bc-green hover-effect">
 </add-new-button>

TypeScript:

onModalClick() {
    if (!this.isOpenModal) {
      this.openModal('new');
    }
}

2 Comments

Where I should set tu true "isOpenModal" in this case?
This work, but where I should put "isOpenModal = flase" ? Cus When I close the modal, then I can't open again this modal on addNew button. Cus isOpenModal stay on true: public onModalClick() { if (!this.isOpenModal) { this.openModal('new'); this.isOpenModal = true; } }
1

here is disable attribute

<my-date-picker [options]="myOptions" [disabled]="disabled" 
                (dateChanged)="onDateChanged($event)"></my-date-picker>

it may be helpful ;)

Comments

0

You can use the CSS selector [ng-reflect-disabled] to trigger the disabled value.

and add [disabled]="isOpenModal" instead of [attr.disabled]="isOpenModal" in your HTML file.

and in your CSS file add the below code:

add-new-button[ng-reflect-disabled="true"] {
  cursor: default;
  pointer-events: none;
}

1 Comment

Just a heads up, you shouldn't use any ng-reflect-... selector as they are only for debugging purposes

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.