Is it possible to invoke a function from one controller in another controller? I attempted the following but encountered failure:
<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 called, I used $scope.$emit("MyFunction");
to trigger Ctrl1's MyFunction
but it did not work. Is this scenario not feasible?
Please shed some light on this matter.