0

I'm wondering and can't find an answer on why is the transcluded content of a directive bound to a child scope of the outer scope, and not to the outer scope itself.

I'm not talking about isolate scope or the scope created with scope: true.

It seems to me that it would've been more intuitive to use the outer scope, so that setting primitive properties on it would work as expected (I know about 'the dot' etc., but still, why not keep things simple).

<div ng-controller="Parent" ng-init="count=0">
   Current count: {{count}}
   <directive-with-transclude>
       <!-- won't work because the count is set in a child scope of Parent's scope -->
       <a href ng-click="count++">Increment</a>
   </directive-with-transclude>
</div>

Is there a reason for that decision?

cipak
  • 1,414
  • 1
  • 14
  • 20
  • `$parent.count` this one works? – Kostia Mololkin Oct 30 '14 at 10:25
  • yes it does, and it can be made to work in various ways, I'm just wondering why did Angular decide to do things this way and not the other way (both ways are in my question:) ). – cipak Oct 30 '14 at 10:36
  • can you set up plunk so we can play around it ,and figure out what's in – Kostia Mololkin Oct 30 '14 at 13:31
  • my question is a theoretical one, i don't think a plunker would be of much use. – cipak Oct 31 '14 at 16:30
  • possible duplicate of [why angular.js transclude child childScope not inherit the parentScope?](http://stackoverflow.com/questions/25535524/why-angular-js-transclude-child-childscope-not-inherit-the-parentscope) – Paul Sweatte Jul 25 '15 at 03:45
  • no, i don't think my question is a duplicate of that question. The transclude scope _does_ inherit from the parent scope, so the question you mention makes a false assumption (https://en.wikipedia.org/wiki/Complex_question). My question is why a child scope of the parent scope is generally needed, and why isn't the parent scope used directly in the directive. – cipak Jul 26 '15 at 13:31
  • a related question is this one: http://stackoverflow.com/questions/17927476/can-i-make-the-transcluded-scope-of-a-directive-be-the-same-as-the-parents. But the poster asks for a way to make the transclude directive scope be the parent scope. I am asking a slightly different question, why did the Angular team decide, and what is the benefit, of having the directive scope be a _child_ of the parent scope (which, granted, inherits from the parent scope, but is not the same). – cipak Jul 26 '15 at 13:43

0 Answers0