0

I have the following directive for a drop down menu (http://jsfiddle.net/77f4m6n5/2/):

<a href="#" dropdown>Open
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
  </ul>
</a>

And the directive is the following:

app.directive("dropdown", dropdown);

function dropdown() {

  var dropdown = {
    link: link,
    replace: false,
    restrict: "A"
  };

  return dropdown;

  function link(scope, element, attributes) {   
    element.bind("click", function(event)   {                                                
        element.children().toggleClass("active");
   });  
  } 
} 

Can I create such a directive but more in "an angular way"? I think I should have a directive for the link and another for the dropdown, no?

1
  • You could look into ng-click, ng-show/ng-hide Commented Apr 23, 2016 at 16:46

1 Answer 1

1

This would be the more angular way of doing it:

.directive('dropdown', function() {
  return {
    link: function(scope, element, attrs) {
      element.bind("click", function(event)   {                                                
        element.children().toggleClass("active");
    },
    replace: false,
    restrict: 'A'
  };
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.