27.03.2017 Views

ng-book

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Controllers 28<br />

$rootScope. If it doesn’t find it on the $rootScope, then it moves on and is unable to update the<br />

view.<br />

To see this behavior in action, let’s create a ParentController that contains the user object and a<br />

ChildController that wants to reference that object:<br />

app.controller('ParentController', function($scope) {<br />

$scope.person = {greeted: false};<br />

});<br />

app.controller('ChildController', function($scope) {<br />

$scope.sayHello = function() {<br />

$scope.person.name = "Ari Lerner";<br />

$scope.person.greeted = true;<br />

}<br />

});<br />

If we bind the ChildController under the ParentController in our view, then the parent of<br />

the ChildController’s $scope object will be the ParentController’s $scope object. Due to the<br />

prototypal behavior, we can then reference data on the ParentController’s containi<strong>ng</strong> $scope on<br />

the child scope.<br />

For instance, we can reference the person object that is defined on the ParentController inside the<br />

DOM element of the ChildController.<br />

<br />

<br />

Say hello<br />

<br />

{{ person }}<br />

<br />

Nested controllers

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!