我想知道是否可以从另一个控制器调用一个控制器的功能。这种方式我试过但失败了:
<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");
}
});
当 func2 调用时,我曾经$scope.$emit("MyFunction");
调用 Ctrl1MyFunction
但它不起作用。那不可能吗?
请在这里放一些光。