AngularJS Module 외부에서 Controller 내 함수 호출하는 방법

jQuery 와 AngularJS 를 같이 사용하고 있다면 혹시나 필요할 때가 있을 수도 있습니다.

HTML 코드

1
2
3
4
5
<div id="Container" ng-app="MainApp" ng-controller="MainController">
    <span>
        Module 외부에서 Controller 내 함수 호출하는 방법
    </span>
</div>

JS 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
angular.module('MainApp', [])
 
.controller('MainController', function($scope) {
    $scope.start = function() {
        alert('Scope Start Function');
    }
});
 
function Scope() {
    var scope = angular.element(document.getElementById("Container")).scope();
 
    return scope;
}
 
$(function () {
    Scope().$apply(function () {
        Scope().start();
    });
});