简体   繁体   English

如何在组件选择器上添加禁用属性?

[英]How to add disable attribute on component selector?

I am trying to disable button.我正在尝试禁用按钮。 But button is angular component.但是按钮是 angular 组件。 And Html5 disabled attribute cannot work on component selector.并且 Html5 disabled 属性不能作用于组件选择器。

I tried to add like this but does not work: [attr.disabled]="isOpenModal我尝试这样添加但不起作用: [attr.disabled]="isOpenModal

Button Html code:按钮 Html 代码:

 <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?任何解决方案的想法?

Because your add-new-button component can be anything, and disabled is not a property that all elements have, that can't work. 因为您的add-new-button组件可以是任何东西,并且disabled不是所有元素都具有的属性,所以它不起作用。 Check out the list of Global Attributes . 签出“ 全局属性 ”列表。

You have to define your own disabled property: 您必须定义自己的disabled属性:

@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>

Just put the disabled logic into the click method itself: 只需将禁用的逻辑放入click方法本身即可:

Template: 模板:

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

TypeScript: 打字稿:

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

here is disable attribute 这是禁用属性

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

it may be helpful ;) 这可能会有所帮助;)

You can use the CSS selector [ng-reflect-disabled] to trigger the disabled value.您可以使用 CSS 选择器[ng-reflect-disabled]来触发禁用值。

and add [disabled]="isOpenModal" instead of [attr.disabled]="isOpenModal" in your HTML file.并在 HTML 文件中添加[disabled]="isOpenModal"而不是[attr.disabled]="isOpenModal"

and in your CSS file add the below code:并在您的 CSS 文件中添加以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM