子作用域发送数据给父作用域 $on $emit
程序员文章站
2022-07-15 10:38:29
...
直接贴代码
<title>无标题文档</title>
<style>
#father{
width:500px;
height:300px;
border:2px solid #ccc;
}
#child{
width:200px;
height:200px;
border:1px dashed #000;
}
</style>
</head>
<script src="angular-1.6.8/angular.js"></script>
<body ng-app="myApp" >
<div ng-controller="father" id="father">
父作用域
<div ng-controller="child" id="child">
子作用域
<input type="button" value="emit" ng-click="postEvent()"/>
</div>
</div>
<script>
var app = angular.module("myApp",[]);
app.controller("father",function($scope){
$scope.$on("info",function(event,data){
console.log("接收到子作用域中的数据");
console.log(data);
});
});
app.controller("child",function($scope){
$scope.postEvent = function(){
$scope.$emit("info","子作用域传递过去的数据");
}
});
</script>
</body>
展示一下截图
上一篇: 枚举