功能 ParentCtrl($scope) {
//一些函数检查子范围内的数据对象是否仍然为空
}
函数 ChildCtrl($scope) {
$scope.data={};
$scope.func = function(){
$scope.data.x = 1;
}; };
jsFiddle:http: //jsfiddle.net/JHwxP/74/
功能 ParentCtrl($scope) {
//一些函数检查子范围内的数据对象是否仍然为空
}
函数 ChildCtrl($scope) {
$scope.data={};
$scope.func = function(){
$scope.data.x = 1;
}; };
jsFiddle:http: //jsfiddle.net/JHwxP/74/
您可以使用如下所示的事件系统:http: //jsfiddle.net/patxy/RAVFM/
如果您有 2 个具有共享服务的控制器,则可以这样做:
var myModule = angular.module('myModule', []);
myModule.factory('mySharedService', function($rootScope) {
var sharedService = {};
sharedService.message = '';
sharedService.prepForBroadcast = function(msg) {
this.message = msg;
this.broadcastItem();
};
sharedService.broadcastItem = function() {
$rootScope.$broadcast('handleBroadcast');
};
return sharedService;
});
function ControllerZero($scope, sharedService) {
$scope.handleClick = function(msg) {
sharedService.prepForBroadcast(msg);
};
$scope.$on('handleBroadcast', function() {
$scope.message = sharedService.message;
});
}
function ControllerOne($scope, sharedService) {
$scope.$on('handleBroadcast', function() {
$scope.message = 'ONE: ' + sharedService.message;
});
}
function ControllerTwo($scope, sharedService) {
$scope.$on('handleBroadcast', function() {
$scope.message = 'TWO: ' + sharedService.message;
});
}
ControllerZero.$inject = ['$scope', 'mySharedService'];
ControllerOne.$inject = ['$scope', 'mySharedService'];
ControllerTwo.$inject = ['$scope', 'mySharedService'];