1

I've been playing with AngularJS for a few days but I still can't grasp some things about the way directives are managed, and I could not find any good answer anywhere for what I'm looking for.

Let's say I have a specific div in which I want to add a directive on a specific event, e.g. on a click. What is the best way to do so ? Or is my approach totally wrong, and should this be done in another way ?

I've set up a Plunker to better illustrate my problem : http://plnkr.co/edit/KM2KFVb6iuhtQKrLYax1?p=preview

Thanks in advance for your help!

1 Answer 1

2

This question partly explains why your method doesn't work: How to pass custom directive name dynamically in class attribute of div tag?

Also, that is not the angular-esque way of doing things. I suspect this would be more angular like while doing what you want: http://plnkr.co/edit/fNZvZ1vzQYQLjZti37GQ?p=preview

Template

<div class="whatever" ng-if="!directiveOn">div.whatever</div>
<div newstuff ng-if="directiveOn">div.whatever</div>

Controller

  $scope.directiveOn = false;
  $scope.click = function() {
    $scope.directiveOn = true;
  };

The other option would be to pass the directive the information of whether it is turned on or off.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This is exactly the solution I was looking for. It makes perfect sense from an Angular point of view.

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.