HTML代码
<div ng-controller="MyAccountCtrl"> <div ng-controller="TransferCtrl"> ............. </div> </div>
js代码
// 子级传递数据给父级 // 子级传递 $scope.checkLoggedIn = function(type) { $scope.transferType = type; $scope.$emit('transfer.type', type); } // 父级接收 $scope.$on('transfer.type', function(event, data) { $scope.transferType = data; }); $scope.checkLoggedIn = function() { var type = $scope.transferType; }
js代码
// 父级传递数据给子级 // 父级传递 $scope.transferType = ''; $scope.checkLoggedIn = function(type) { $scope.transferType = type; $scope.$broadcast('transfer.type', type); } // 子级接收 $scope.transferType = ''; $scope.$on('transfer.type', function(event, data) { $scope.transferType = data; }); $scope.checkLoggedIn = function() { var type = $scope.transferType; }
应用实例:未读消息的个数显示
子级控制器js代码:
1 $scope.messageView = function(data){ 2 $scope.currentMessage = data; 3 ngDialog.open({ 4 template: 'html/admin/messageView.html', 5 className: 'ngdialog-theme-plain custom-width-70', 6 scope: $scope, 7 cache: false, 8 controller: function(){ 9 if(data.readStatus == 0){ //状态为0表示未读; 10 $scope.$emit('messageCount', true); //等于0的时候触发 11 } 12 } 13 }); 14 if (data.readStatus == 1) return; 15 data.readStatus = 1; 16 $http.post($scope.URL + "message/updateMessage", data).success(function(){ 17 $scope.load(); 18 }); 19 };