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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)