话不多说,上代码
<script src="angular-1.6.8/angular.js"></script>
<body ng-app="myApp">
<div ng-controller="father">
<input type="text" ng-model="father"/>
<input type="button" ng-click="father_click()" value="广播"/>
<div ng-controller="child">
<input type="text" ng-model="child"/>
<input type="button" ng-click="child_click()" value="消息"/>
</div>
</div>
<script>
var app = angular.module("myApp",[]);
app.controller("father",function($scope){
/*父亲发送广播*/
$scope.father_click = function(){
$scope.$broadcast("father",$scope.father);
}
/*父亲接收消息*/
$scope.$on("child",function(event,data){
$scope.father = data;
});
});
app.controller("child",function($scope){
/*儿子发送消息*/
$scope.child_click = function(){
$scope.$emit("child",$scope.child);
}
/*儿子接受广播*/
$scope.$on("father",function(event,data){
$scope.child = data;
});
});
</script>
</body>
发消息:
/*儿子发送消息*/
$scope.child_click = function(){
$scope.$emit("child",$scope.child);
}
发广播:
/*父亲发送广播*/
$scope.father_click = function(){
$scope.$broadcast("father",$scope.father); //$scope.father-->文本框中的数据
}
收消息:
/*父亲接收消息*/
$scope.$on("child",function(event,data){
$scope.father = data;
});