<div ng-controller="MyAccountCtrl">
<div ng-controller="TransferCtrl">
.............
</div>
</div>
// 子级传递数据给父级
// 子级传递
$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;
}
// 父级传递数据给子级
// 父级传递
$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;
}
在线实例:http://each.sinaapp.com/angular/apps/app-broadcast.html
本文介绍了AngularJS中实现父子组件间数据通信的方法。通过使用$scope提供的$broadcast, $on和$emit方法,详细展示了如何从子级向父级以及从父级向子级传递数据的具体实践。
513

被折叠的 条评论
为什么被折叠?



