Search code examples
angularjsmaterializedropdown

AngularJS + Materialize dropdown doesn't work


I have a problem... My Materialize dropdown works only after a reloading of my homepage. I tried all the solutions which I was able to find. Nothing can be done.

Here is my code :

this.$postLink = () => {
      $timeout(() => {
        $('.dropdown-button').dropdown();
      }, 1000);
    };
<li ng-if="$ctrl.parent.isLogged()">
    <a class="dropdown-button" href data-activates="menu_user">
       {{'NAVBAR.HELLO' | translate}} {{$ctrl.parent.userName()}}
           <i class="material-icons right">arrow_drop_down</i>
    </a>
</li>

Thanks for your help ;) !


Solution

  • You have to use a directive:

    View

    <div ng-controller="MyCtrl">
    
      <a class='dropdown-button btn' 
         href='#' 
         data-activates='dropdown1' 
         my-dropdown-button>Drop Me!</a>
    
      <ul id='dropdown1' class='dropdown-content'>
        <li><a href="#!">one</a></li>
        <li><a href="#!">two</a></li>
        <li class="divider"></li>
        <li><a href="#!">three</a></li>
      </ul>
    </div>
    

    AngularJS application

    var myApp = angular.module('myApp', []);
    
    myApp.controller('MyCtrl', function() {});
    
    myApp.directive('myDropdownButton', function() {
      return {
        restrict: 'A',
        link: function(scope, element) {
          element.dropdown({
            inDuration: 300,
            outDuration: 225,
            constrainWidth: false, // Does not change width of dropdown to that of the activator
            hover: true, // Activate on hover
            gutter: 0, // Spacing from edge
            belowOrigin: false, // Displays dropdown below the button
            alignment: 'left', // Displays dropdown with edge aligned to the left of button
            stopPropagation: false // Stops event propagation
          });
        }
      }
    });
    

    > demo fiddle