0

I'm trying to call a function that sits inside a angular controller from outside the controller. My code looks similar to this:

var myApp = angular.module('myApp'[])
  .controller('MainController', ['$scope', function($scope) {
    $scope.message = function() {
      alert('Hello World');
    };
}]);

function talk() {
  // message()
}

What I want to accomplish is to access my message() function from inside my talk() function.

Because in my application there is a lot of code involved, that would be the easiest way for me, but if you know a better solution please tell.

Thanks!

André Kuhlmann
  • 4,378
  • 3
  • 23
  • 42

1 Answers1

0

Its bad practise to use code from a controller out side its scope. But you should be able to get the "scope" if you are able to select they element in the DOM. not sure what version of angular your using

depending on your angular version:

var scope = angular.element(document.getElementById('yourMainControllerElementID')).injector().‌​get('$rootScope')

or

angular.element(document.getElementById('yourMainControllerElementID')).scope().message();

.get returns the scope of the controller.

read more over here: https://stackoverflow.com/a/16737459/1392634

Community
  • 1
  • 1
  • Thanks for your answer, sadly it didn't work. You sad this method is very bad practise so I will take some time and restructure my App. – André Kuhlmann Oct 31 '16 at 18:52