1

I have a routeprovider that assigns a controller to a different route.

.when('/page/:id', {
  templateUrl: '/page/overview.ejs',
  title: __('pageTitle'),
  controller: 'PageCtrl'
}

and in the overview.ejs I have something like this:

<div>
     <div ng-controller="SubPageCtrl">
     ....
     </div>
</div>

The question is: how do I access the property of PageCtrl in SubPageCtrl?

I tried to do this, but it does not work:

function PageCtrl ($scope) {
    $scope.test = "testing";
}

function SubPageCtrl($scope) {
    console.log($scope.$parent.test);
    console.log($scope.test)
}

Both of them print out undefined. Does anybody know how I can make it work?

Edit:

It turned out I put the $scope.test = "testing"; in a callback of a asynchronous call, so the data is not ready when I call the console.log.

Other than that, it should be just what Vignesh's response below

Chun Tai
  • 55
  • 3
  • You can't pass data like that in angular, for that you have to use $broadcast to share data between 2 controller. – Maher Jan 13 '16 at 05:20
  • i answered this question, look at this [share data between 2 controller](http://stackoverflow.com/questions/34668416/linking-one-controller-to-another-to-call-service-on-ng-click/34702780#34702780) – Maher Jan 13 '16 at 05:23
  • Thanks for the reference, Maher. Although, this is just a simple parent-to-child structure. We can just use the share scope – Chun Tai Jan 13 '16 at 05:37

1 Answers1

0

Use can try this

In PageCtrl Declare $scope.test as an object. $scope.test = {data: 'test'}

In SubPageCtrl you can directly able to access the '$scope.test.data'. You don't need to use the $parent.

Make use of prototype chaining.

Vignesh Sn
  • 110
  • 8