Angular2, what is the correct way to disable an anchor element?

前端 未结 10 1214
死守一世寂寞
死守一世寂寞 2020-11-27 17:51

I\'m working on an Angular2 application, and I need to display -- but disable an HTML element. What is the corr

相关标签:
10条回答
  • 2020-11-27 18:30

    My answer might be late for this post. It can be achieved through inline css within anchor tag only.

    <a [routerLink]="['/user']" [style.pointer-events]="isDisabled ?'none':'auto'">click-label</a>

    Considering isDisabled is a property in component which can be true or false.

    Plunker for it: https://embed.plnkr.co/TOh8LM/

    0 讨论(0)
  • 2020-11-27 18:31
       .disabled{ pointer-events: none }
    

    will disable the click event, but not the tab event. To disable the tab event, you can set the tabindex to -1 if the disable flag is true.

    <li [routerLinkActive]="['active']" [class.disabled]="isDisabled">
         <a [routerLink]="['link']" tabindex="{{isDisabled?-1:0}}" > Menu Item</a>
    </li>
    
    0 讨论(0)
  • 2020-11-27 18:33

    I have used

    tabindex="{{isEditedParaOrder?-1:0}}" 
    [style.pointer-events]="isEditedParaOrder ?'none':'auto'" 
    

    in my anchor tag so that they can not move to anchor tag by using tab to use "enter" key and also pointer itself we are setting to 'none' based on property 'isEditedParaO rder' whi

    0 讨论(0)
  • 2020-11-27 18:35

    You can try this

    <a [attr.disabled]="someCondition ? true: null"></a>
    
    0 讨论(0)
提交回复
热议问题