Angular Dropdown directive doesn't work

前端 未结 2 1957
遥遥无期
遥遥无期 2021-01-05 20:10

I am using Angular 6.0.5 alongside with Bootstrap 4.1.1 and I added a directive for dropdowns. But I can\'t make it work in no way. I have seen a lot of similar problems her

相关标签:
2条回答
  • 2021-01-05 20:11

    Make sure you have imported your directive to app.module.ts file

    0 讨论(0)
  • 2021-01-05 20:23

    In order to work drop down in bootstrap you need to add show class in two places which you can achieve by get the reference to isOpen variable using angular template ref

    If you want to get the reference to directives you need to export the directive first

    @Directive({
      selector: '[appDropDown]',
    exportAs:'appDropDown'
    })
    

    Then you can use the template ref method to achive dropdown

    <div class="btn-group" appDropDown #r="appDropDown" >
      <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" >
        Manage
        <span class="caret"></span></button>
      <ul class="dropdown-menu" [ngClass]="{'show':r.isOpen}" >
        <li><a class="dropdown-item" style="cursor: pointer"
               >To Shopping List</a></li>
        <li><a class="dropdown-item" href="#">Edit Recipe</a></li>
        <li><a class="dropdown-item" href="#">Delete Recipe</a></li>
      </ul>
    </div>
    

    I have include example here check out: https://stackblitz.com/edit/angular-h9rgmn

    0 讨论(0)
提交回复
热议问题