我不确定你想在这里实现什么。
只要它们不相关,跨多个指令共享控制器就不是一个好主意。您应该为此使用服务。
您可以参考以下链接以了解如何建立此通信
https://github.com/ynmanware/AngTest/tree/master/Angular-unrelated-controller-messaging
我在 plnkr 上贴了一个精炼的例子
http://plnkr.co/edit/den1mfMeIAWezLTuVZVX?p=preview
html
<body>
<div ng-app="myApp">
<div ng-controller="Controller1 as ctrl">
<br>
<table>
<tr>
<td><B> Enter Description in Controller1</B></td>
<td><input type="text" ng-model="ctrl.description"></td>
</tr>
</table>
</div>
<div ng-controller="Controller2 as ctrl">
<table>
<tr>
<td><B> Description in controller2</B></td>
<td>{{ctrl.description}}</td>
</tr>
</table>
</div>
</div>
</body>
.js
(function(app) {
function SharedService() {
var description = "Master";
}
angular.extend(SharedService.prototype, {
getDescription: function() {
return this.description;
},
setDescription: function(desc) {
this.description = desc;
}
});
function Controller1(SharedService) {
this.sharedService = SharedService;
}
Object.defineProperty(Controller1.prototype,
'description', {
enumerable: true, //indicate that it supports enumerations
configurable: false, //disable delete operation
get: function() {
return this.sharedService.getDescription();
},
set: function(val) {
this.sharedService.setDescription(val);
}
});
function Controller2(SharedService) {
this.sharedService = SharedService;
}
Object.defineProperty(Controller2.prototype,
'description', { //read only property
enumerable: true,
configurable: false,
get: function() {
return this.sharedService.getDescription();
}
});
app.service('SharedService', SharedService);
app.controller('Controller1', Controller1);
app.controller('Controller2', Controller2);
})(angular.module('myApp', []));