Read more

Angular: Understanding how parent and child scope meet on a directive declaration

Dominik Schöler
March 26, 2015Software engineer at makandra GmbH

This cards needs to be rewritten.


Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

Say you have the following markup:

{{ someParentVariable = 42 }}

<child var="{{ someParentVariable }}" ng-click="foo()">
  {{ var }}
</child>

Any angular expression on the child element will be evaluated
before child is initialized. Hence, {{ }} is always evaluated in the
parent scope.
attributes.foo will be "42" inside the child directive
(as a string, because HTML attributes are always strings).

scope: true

If the child directive sets scope:true, a new child scope is
attached to the directive's element. It is available to any
other Angular expression on that element
. ngClick will invoke
the foo() function that the child directive defines.

scope: {}

Now assume the child directive defines an isolate scope. This isolate
scope will be truly isolate and not accessible to other Angular
expressions or directives on that element
. See this Stackoverflow
answer
, section "Breaking Change" (Angular 1.2+). ngClick will
invoke the foo() function defined on the parent scope.

Posted by Dominik Schöler to makandra dev (2015-03-26 13:29)