How to enable/disable dropdown with angular2?

Hope it is not a duplicate question. I have some layout which is as follows,

<div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Category <span class="caret"></span>
   </button>
   <ul class="dropdown-menu default-dropdown">
         <li><a href="#">Action</a></li>
         <li><a href="#">Another action</a></li>
         <li><a href="#">Something else here</a></li>
   </ul> 
 </div>

How to enable dropdown on button click with angular?

Answer

To enable/disable

<div class="btn-group">
    <button [attr.disabled]="dropdownDisabled ? true : null" 
            type="button" class="btn btn-default dropdown-toggle" 
            data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Category <span class="caret"></span>
   </button>
   <ul class="dropdown-menu default-dropdown">
         <li><a href="#">Action</a></li>
         <li><a href="#">Another action</a></li>
         <li><a href="#">Something else here</a></li>
   </ul> 
 </div>

 <button (click)="dropdownDisabled = !dropdownDisabled">toggle</button>

to open/close

i assume that you’re using bootstrap css style

<div class="btn-group dropdown" [class.open]="dropdownOpened">
    <button (click)="dropdownOpened = !dropdownOpened"
            type="button" class="btn btn-default dropdown-toggle" 
            data-toggle="dropdown" aria-haspopup="true" 
            [attr.aria-expanded]="dropdownOpened ? 'true': 'false' ">
    Category <span class="caret"></span>
   </button>
   <ul class="dropdown-menu default-dropdown">
         <li><a href="#">Action</a></li>
         <li><a href="#">Another action</a></li>
         <li><a href="#">Something else here</a></li>
   </ul> 
 </div>

Leave a Reply

Your email address will not be published. Required fields are marked *