3

ngIf creates another scope inside parent scope. According to angular documentation, as I understand from this link, is passes inside scope to parent scope in theory. But in practice, it does not. I need to pass this scope features to parent scope. How can I do that.

Thanks for any help.

xxlali
  • 996
  • 2
  • 15
  • 43
  • Can you create [Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve)? – Satpal Nov 16 '15 at 13:13
  • 2
    Please create an example, also I recommend using 'Controller As', and dropping scope. It will fix a lot of these issues. – Nick Delaney Nov 16 '15 at 13:37

1 Answers1

4

There are many ways to address this one if i understood you question (i tried). Alternatives provided.

Approach 1 :

you can use $parent inside ng-if and do the rest .

Approach 2 :

you just need to modify the structure into object notation i.e {obj.yourval}.

<body ng-app="ngAnimate" ng-init="cool.mess='Im removed';checked=true">
   <br/> : {{cool.mess}}
   <br/> Click me: <input type="checkbox" ng-model="checked" /><br/>
  Show when checked:
  <span ng-if="checked" class="animate-if">
   <input type="text" ng-model="cool.mess"/>
  </span>
</body> 

sample up for grabs here .

Approach 3 :

you can use ng-hide instead of using ng-if which solves all mysteries (IMHO be this you buddie if you have nothing to do with UI re-structuring ).

PS: just rounding up , docs clearly say if used ng-if it creates a child scope . so child can access parents data not the other way around . thats the reason why modification in child structure is not reflected at parents level . use anyone of the above alternatives .

super cool
  • 6,003
  • 2
  • 30
  • 63
  • I need to not to compile the code if ngIf experession false because it impacts the functionality not the ui so approach 3 is not the solution I am looking for. I could not understand approach 2. How this resolves the problem. Also, I am not fully comfortable with approach 1 as well. What is the advantage of $parent using inside ng-if. Thanks. – xxlali Nov 16 '15 at 14:20
  • @xxlali usually `$parent` gets you the parents level data when accessed from inside child level . see again i'm stressing `inside` child as you are aware inside child scope it can have access to `only` child's data . so we use `$parent` inside child to get hold of parents data . #ng-if creates a separate instance (if used) . – super cool Nov 16 '15 at 15:20
  • I did not have time to try. When I try, if this solves my problem, I mark. Don't worry. – xxlali Nov 18 '15 at 14:47