dman dman - 16 days ago 5
AngularJS Question

Scope property exists in child controller when it should not

I have a child controller. In the parent controller

$scope.dealers
exist, but not in the child controller. How is it possible that
$scope.dealers
is existing in the child controller?

If you look in
watch
expression, you will see the
$scope.dealers
exist while in the
Closure
the
$scope
does not have dealers.

enter image description here

Answer

It's a "feature" of angular scopes. Child scope prototypically inherits from the parent scope.

From https://docs.angularjs.org/guide/scope - Scope Hierarchies section

When Angular evaluates {{name}}, it first looks at the scope associated with the given element for the name property. If no such property is found, it searches the parent scope and so on until the root scope is reached. In JavaScript this behavior is known as prototypical inheritance, and child scopes prototypically inherit from their parents.

Comments