AngularJS : Climb up the scope tree in an ng-repeat -


i build flyout tree menu adds active class elements on hover, , removes when of siblings hovered.

this way, menu stay open when user mouses away it.

the flyout menu recursive because number of levels deep.

in order remove class siblings, must have knowledge of parent or sibling status. prefer data structure, use dom traversal if needed.

i have tried:

  • using scope.$parent in hover handler (but scope handler knows directive scope, not ng-repeat scope)
  • passing event hover handler , calling $(evt).siblings() (but angular yells, saying cannot access dom expression)
  • setting .parent property in local ng-repeat scope (but event handler not have access these values template inherits)
  • using recursive directive, every leaf in tree directive. explodes browser. (i think because tries compile directive before looking exit condition on loop)
  • adding controller every leaf in tree. makes scope tree large, , doesn't provide clean way access siblings. functional though. see answer below.

i have no idea how in angular. in jquery simple: $(e.target).siblings().removeclass('active'). how can done?

i've made plunker demonstrate issue accessing siblings in recursive ng-repeat: http://plnkr.co/edit/vbyyt6stvwkn9txz9sc5?p=preview

a great solution uses variable track active leaf @ each level of tree. in method 1 declares variable in parent scope , assigns in leaf scope. 1 sibling active @ given time: ng-class="{active: branch.name == activeleaf}".

plunkr written cam[tab] #angularjs: http://plnkr.co/edit/u5eip2dmqtbroxl7qq53?p=preview

you use controller above each ng-repeat declare scope, adds lots of scope layers. use nested directives contain scope, recurse infinitely. cam's solution, linked above, includes nested directive in ng-include, generates nice lean scope tree , avoids infinite recursion.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -