Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access parent property using Controller As notation

Tags:

angularjs

I'm using the Controller as in my view as follows:

<body ng-controller="MainCtrl as main">    <div ng-controller="ChildCtrl as child">       {{ main.parentValue }} + {{ child.childValue }}    </div> </body> 

Defining my controller like this:

app.controller('MainCtrl', function($scope) {    this.parentValue = 'Main'; });  app.controller('ChildCtrl', function($scope) {    this.childValue = 'Child';    // I want to access the property of the parent controller here }); 

How can the ChildCtrl set the name property of the MainCtrl? Here the Plunkr.

Using the $scope notation, I could have accessed $scope.parentValue from the child controller. How can the same functionality be achieved using the Controller As notation?

like image 344
bengro Avatar asked Oct 10 '14 23:10

bengro


People also ask

Can parent controller access the methods of child controller or vice versa?

39) Do you think that parent controller can access the methods of child controller or vice versa? No, the parent controller cannot access the methods of child controller, but the child controller can access the methods of the parent controller.

What is scope parent?

Parent and Child Scopes You can create a new child scope by calling a script or function. The calling scope is the parent scope. The called script or function is the child scope. The functions or scripts you call may call other functions, creating a hierarchy of child scopes whose root scope is the global scope.

What is scope on in AngularJS?

AngularJS Scope The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.


2 Answers

Since your using the "controller as" notation, witin your ChildCtrl you can access the MainCtrl using $scope.main, for example $scope.main.name.

See my snippet below.

var app = angular.module('app', []);    app.controller('MainCtrl', function($scope) {    this.name = 'Main';    this.test = {};  });    app.controller('ChildCtrl', function($scope) {    this.name = 'Child';    alert($scope.main.name);  });
<html ng-app="app">  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>    <body ng-controller="MainCtrl as main">    <div ng-controller="ChildCtrl as child">      {{ main.name }} + {{ child.name }}    </div>  </body>    </html>
like image 121
JME Avatar answered Oct 07 '22 15:10

JME


You should not mix up "controller as" and $scope usage. To update data in a parent-scope you could/should use services.

Example: Changing the page-title from within any Child-Controller:

app.service("SiteService", function () {     return {         title: "Page title";     } }   app.controller ("ParentCtrl", function (SiteService) {     this.site = SiteService; }  app.controller ("ChildCtrl", function (SiteService) {     SiteService.title = "New Title"; } 

And your template

<html ng-app="someApp" ng-controller="ParentCtrl as site">     <head>          <title>{{site.title}}</title>     </head> </html> 

The main advantage of this approach: You separate public mutable from private properties.

like image 33
Matthias Leuffen Avatar answered Oct 07 '22 15:10

Matthias Leuffen