0

Tried so many different ways and still can't figure this. I have a menu with ng-class="{menuVisibleAnimation: menuOpen}" in a template in a nested directive. When I click on the button in the parent directive I want to change the value of menuOpen to true but the menu in the child directive is not updating?

http://plnkr.co/edit/nOunKkch0Gt8hjMWtruA?p=preview

2
  • When i click the button, the class is toggling on and off for the div: <div class="view menuVisibleAnimation" data-ng-class="{menuVisibleAnimation: menuOpen}"><p>the view</p></div> Commented Jun 21, 2013 at 0:30
  • The idea is that when I click on the button both the view div and <div class="menu" data-ng-class="{menuVisibleAnimation: menuOpen}" data-ng-transclude></div>. As you said, only the view div is toggling but not the menu Commented Jun 21, 2013 at 8:24

1 Answer 1

1

The main issue in your implementation is that you want to use the the $scope to share the value of menuOpen between the parent and child directive, but your parent directive has an isolated scope :

scope: {
  menuOpen: '@menuOpen'
}

You need to declare menuOpen in a scope shared by both directives, due to transclusion it has to be the parent scope of the parent directive. So, in the parent directive you should not create a new scope :

scope: false,
link: function($scope) {      
  $scope.menuOpen = false;
  $scope.toggleMenu = function() {
    $scope.menuOpen = !$scope.menuOpen;
  };
}

Then, openMenu is accessible in the child directive. See it in in a fork of your Plunker.

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.