I'm trying to get the materialize library to work together with Angular. To do so I've written an attribute directive that will initialize the dropdown menu on an element.
However, the directive throws an exception (see the dev tools) in the case where the data-activates
attribute uses a double-curly-brace expression. Here's a repro (with, for reference, a hard-coded working version as well):
angular.module('foo', [])
.directive('materialKebab', function() {
return {
link: function(scope, element, attrs) {
$(element).dropdown();
}
};
})
.controller('bar', function($scope) {
$scope.id = "abc123";
});
.dropdown-button { cursor: pointer; background: #ddd; padding: 10px; display: inline-block; font-weight: bold; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.js"></script>
<div ng-app="foo" ng-controller="bar">
Doesn't work:
<div class="dropdown-button" data-activates="item_{{id}}" material-kebab>⋮</div>
<ul class="dropdown-content" id="item_{{id}}">
<li>Action 1</li>
<li>Action 2</li>
</ul>
</div>
<hr>
<div>
Hard coded one works:
<div class="dropdown-button" data-activates="hardCodedId">⋮</div>
<ul class="dropdown-content" id="hardCodedId">
<li>Action 1</li>
<li>Action 2</li>
</ul>
</div>
The exception is from jQuery (Sizzle, to be precise):
Error: Syntax error, unrecognized expression: #item_{{id}}
So apparently the dropdown is being activated too early? I've tried setting a priority
for my directive to try and make it happen after the other attribute was properly populated, but that didn't seem to work. I've also checked this Github repo with angular-materialize features, but it seems to have no special consideration for this case either.
Is there any good solution to this? Or do I have to resort to watching the $scope.id
variable before calling dropdown()
?