AngularJS uses prototype JavaScript inheritance so that regions can access the properties of the parent region. You can define controllers embedded in HTML and access the parent element from the child element. However, I urge you not to rely on this fact for your AppCtrl. In some cases, the scope you are working on will be isolated and will not be part of the inheritance hierarchy that has access to the AppCtrl scope.
I would suggest creating a service for this, or you could use pub / sub with $ rootScope. $ on and $ rootScope. $ broadcast.
To show an example of a service, I use the words shellCtrl and a shell service instead of an application to make the example a little understandable.
Defining a shell service should allow any other controller, directive, or service in your application to interact with the shellController and therefore the host view container.
<div ng-app="myApp" ng-controller="ShellCtrl"> <div ng-controller="SomeOtherCtrl"></div> </div> // parent controller defined on the same element as ng-app function ShellCtrl($scope, shell) { // I've just made the shell accessible to the $scope of shellctrl, but you can do // this in various ways. $scope.shell = shell; } // any other controller function SomeOtherCtrl($scope, shell) { shell.setTitle('Some title'); } // basic example of the shell service angular.module('myApp').factory('shell', function () { return { title = 'No title set', setTitle = function (title) { this.title = title; } } });
Now you can set the properties on the parent controller separately, without relying on a hierarchy of regions.
source share