0
          <div  ng-show="level1.collapsed || level2.selected"  ng-repeat="level1 in main"> ...
                <div ng-if="level1.sub"> 
                    <div ng-show="level2.collapsed" ng-repeat="level2 in level1.sub">
                        ...
                    </div>
                </div>
            </div>

I have a nested div-structure and as shown in my code i want to change the visibility of div in higher level depend on a value of lower level div.

In my code, || level2.selected does not recognized. Is that possible to do that using a similar way? Preferably without using any variable which should be defined in a controller.

Asqan
  • 4,319
  • 11
  • 61
  • 100
  • How do you set level2.selected? – Chrillewoodz Aug 03 '15 at 20:30
  • This question will shed light on your problem: http://stackoverflow.com/questions/13428042/angularjs-access-to-child-scope – Chrillewoodz Aug 03 '15 at 20:31
  • You don't need access to child scope. Write a function `levelHasActiveChildren()` and use in `ng-show="level1.collapsed || levelHasActiveChildren(level1)" – Kirill Slatin Aug 03 '15 at 20:42
  • the problem with `levelHasActiveChildren(level1)` is that i take all the elements in level 1 and not only the relevant ones (thus not like in ng-if in my second line. `levelHasActiveChildren(this)` brings also undefined. I believe it will be better if i can reach them without using js. – Asqan Aug 03 '15 at 23:52
  • and it is executed several times. Suppose it is defined in 4. level; it is executed #1 * #2 * #4 times – Asqan Aug 04 '15 at 07:33
  • Are `main` and `main[0].sub` arrays? – Kirill Slatin Aug 04 '15 at 17:35
  • Yes; they are the arrays of my tree – Asqan Aug 05 '15 at 07:25

1 Answers1

0

You cannot access child scope from parent scope. You must do it the other way around.

One solution is to bind level2.selected to the parent scope by using level2.$parent.selected where you set the property, not in || level2.selected. This will make it available in the parent scope from the child scope.

Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
  • 1
    what does it mean to access parent scope where you `set the property`? Setting the property can happen in one scope, for example in controller `main[2].sub[1].selected = true;`. And nested scope in template exist only because of usage of `ng-repeat` directive. There is nothing assigned there – Kirill Slatin Aug 03 '15 at 20:41
  • Can you apply your possible solution to my example? If i am not wrong, your answer is not applicable because i have no any `selected` attribute in my parent in order to change the visibility of him. – Asqan Aug 04 '15 at 09:27