0

I want to access an Angular controller from outside of the controller component.

This is answered well in this question: AngularJS. How to call controller function from outside of controller component

However, what if I am using ngRoute with code like this:

myApp.config(function ($routeProvider) {

    $routeProvider

    .when('/', {
        templateUrl: 'pages/main.html',
        controller: 'mainController'
    })

    .when('/second', {
        templateUrl: 'pages/second.html',
        controller: 'secondController'
    })

});

So the index page only has this tag: <div ng-view></div>.

Is there a way I can access the controller that is currently in the ng-view?

(I tried to locate mainController on the index page, but for:

var scope = angular.element(document.getElementById("mainController")).scope();

scope is undefined.)

Community
  • 1
  • 1
RaelB
  • 3,301
  • 5
  • 35
  • 55

0 Answers0