I was wondering if is it possible to call one controller's function from another controller. This way i tried but failed:
<div ng-app="MyApp">
<div ng-controller="Ctrl1">
<button ng-click="func1()">func1</button>
</div>
<br>
<div ng-controller="Ctrl2">
<button ng-click="func2()">func2</button>
</div>
</div>
var app = angular.module('MyApp', []);
app.controller('Ctrl1', function($scope) {
$scope.func1 = function() {
alert('Ctrl1 and func1')
}
$scope.$on("MyFunction", function () {
alert('Ctrl1 MyFunction')
});
});
app.controller('Ctrl2', function($scope) {
$scope.func2 = function() {
alert('Ctrl2 and func2')
$scope.$emit("MyFunction");
}
});
when func2 is calling then i used $scope.$emit("MyFunction");
to call Ctrl1 MyFunction
but it does not worked. so is it not possible ?
please put some light here.