8

I am completely new to AngularJS. In the code I am supposed to add a feature to I can see $scope.$parent I know about $scope. I also know that when I see a $ it means it is built-in angular. So I searched for it in Angular web site but I did not have any luck finding anything about $Parent as a built-in service or factory or directive, etc... Can anybody help me understand what it means. Also how I can get to an answer in their documentation when I run into something new?

Maryam
  • 1,417
  • 4
  • 19
  • 33
  • 1
    Check this out. http://stackoverflow.com/questions/21453697/angularjs-access-parent-scope-from-child-controller – jcc Jan 27 '16 at 00:45
  • 1
    most of the time you see $parent .. it's from poor practices. It has been abused a lot – charlietfl Jan 27 '16 at 00:47

2 Answers2

15

The documentation for $parent is sparse, but you can find it referenced here at the very bottom of the page.

$scope.$parent refers to the $scope of the parent element. E.g. if you have an element with a controller nested within another element with it's own controller:

<div ng-controller="parentController">
  ... something in the parent element
  <div ng-controller="childController">
     ... something in the child element
  </div>
</div>

You can access variables attached to the parentController from the childController using $scope.$parent. and use them in the child element.

Patrick Shaughnessy
  • 8,850
  • 1
  • 10
  • 6
  • 2
    +adding minor detail: All the parent properties will be accessible in child controller by default with exception of isolated scope. We can make use of $parent where we have a property with same name in both child and parent controller. In this case child property shadows parent controller property. – Diljohn5741 Jan 27 '16 at 06:13
7

In angular, your scopes are all chained together. So, you can reference the scope "above" your immediate scope with $parent.

It's useful, for example, if you are working with a directive (or if you have a controller inside of another).

bri
  • 2,932
  • 16
  • 17