3

http://plnkr.co/edit/0CAgXsX847n9LfIG4Fzj?p=preview

I have two files: -index.html (This uses an ng-include - DOESNT WORK) -index_embedded.html (Uses same code but without using an include - WORKS)

Hi, my problem here is when I hit "CLICK ME", then hit "CLOSE", then try to hit "CLICK ME" again, it no longer works.

When I use the same code without using the ng-include, i'm able to hit "CLICK ME", "CLOSE", and then "CLICK ME" again.

Why is this not working only when the code is in an include file?

<h2 ng-click="asdf = !asdf">CLICK ME</h2>

<!-- <div ng-include="'partials/audiencenav.html'"></div> -->
<!-- extracted code from the include and pasted below, i'm able to successfully  CLICK, CLOSE, and CLICK again -->
<div class="" ng-show="asdf">
    <div ng-click="asdf = !asdf"><h2>CLOSE</h2></div>   
</div>  

The problem is when the code is used placed in an include file, it breaks after hitting "CLOSE".

Venkat.R
  • 7,420
  • 5
  • 42
  • 63
coderaging
  • 47
  • 1
  • 8

3 Answers3

4

First time it is inheriting the value of asdf from parent until you have not clicked on close once you have clicked on CLOSE it is creating asdf with local scope which is no more affected by clicking on CLICK ME, so you can use $parent.asdf in include file which will always refers to parent.

Please see the updated plunker

<div class="" ng-show="$parent.asdf">
    <div ng-click="$parent.asdf = !$parent.asdf"><h2>CLOSE</h2></div>   
 </div>

http://plnkr.co/edit/9wmKTR8HPw54K95XqapA?p=preview

Sardesh Sharma
  • 1,671
  • 1
  • 12
  • 14
2

I think the problem is because ng-include creates new scope and the asdf is shadowed by local version of this scope.

This is well known issue. You can put the variables inside inner struct (e.g change asdf to input.asdf and initialize input to {}) or (more advanced) use the controller as construct.

look here: https://stackoverflow.com/a/14146317/115198

Community
  • 1
  • 1
eran
  • 6,731
  • 6
  • 35
  • 52
2

This is because with ng-include new child scope is created in the included file and outer scope becomes parent scope.

So you should try like below in _include.html

<div class="" ng-show="$parent.asdf">
    <div ng-click="$parent.asdf = !$parent.asdf"><h2>CLOSE</h2></div>   
</div>

Hope it helps!