angular controller与directive相互引用
2024-10-19 17:19:12
/**
* Created by Administrator on 2017/8/28.
*/
var app =angular.module('app',[]);
app.directive('food',function () {
return {
restrict:"E",
scope:[],
controller:function($scope){
$scope.foods=[];
this.addApple=function () {
$scope.foods.push("apple");
}
this.addOrange=function () {
$scope.foods.push("orange");
}
this.addBanana=function () {
$scope.foods.push("banana");
}
},
link:function ($scope,element,attrs) {
element.bind("mouseenter",function () {
console.log($scope.foods)
});
}
}
});
app.directive('apple',function () {
return {
require:'food',
link:function($scope,element,attrs,foodCtrl){
foodCtrl.addApple();
}
}
});
app.directive('orange',function () {
return {
require:'food',
link:function($scope,element,attrs,foodCtrl){
foodCtrl.addOrange();
}
}
});
app.directive('banana',function () {
return {
require:'food',
link:function($scope,element,attrs,foodCtrl){
foodCtrl.addBanana();
}
}
}); app.directive('hello',function(){
return {
restrict:"E",
replace:true,
template:'<div><input type="text" ng-model="txt"/><div>{{txt}}</div></div>',
link:function($scope,element,attrs){
$scope.$watch('txt',function(newVal,oldVal){
if(newVal==="error"){
console.dir(element);
element.attr("style","border:solid 1px red");
}else{
element.attr("style","");
}
});
}
}
}); app.controller('OneSelfController',function($scope){
$scope.clkme=function(){
$scope.$broadcast('sendChild','我给子控制器传递数据');
$scope.$emit('sendParent','冒泡到父元素')
}
}).controller('ParentController',function($scope){
$scope.$on('sendParent',function(event,data){//监听在子控制器中定义的 sendParent 事件
console.log('OneSelfController传过来的',data,event.name,event);//事件名称:sendParent
});
$scope.clkP=function(){
$scope.$broadcast('sendAllChild','让siblingsController接收到');
} }).controller('ChildController', function($scope){
$scope.$on('sendChild', function(event,data) {//监听在子控制器中定义的 sendChild 事件
console.log('ChildCtrl', data,event.name,event);// 事件名称:sendChild
});
}).controller('siblingsController', function($scope){
$scope.$on('sendAllChild',function(event,data) {
console.log('值过来吧', data);
});
//下面事件不会触发
$scope.$on('sendParent', function(event,data) {
console.log('平级得不到值', data);
});
$scope.$on('sendChild', function(event,data) {
console.log('平级得不到值', data);
});
});
最新文章
- java比较版本号
- MFC学习-第一课 MFC运行机制
- phalcon: 查找记录(Finding Records)可用的查询设置如下:
- IFRAM的详细用法
- jQuery iframe 自适应高宽度
- 如何写一个像btgoogle一样的12306泄露数据查询
- hdu just a hook(线段树,区间修改)
- Linux中查看socket状态(转)
- HDU 2489 Minimal Ratio Tree 最小生成树+DFS
- 扩展jquery.validate自定义验证,自定义提示,本地化
- spring boot jpa 使用update 报错解决办法
- [Swift]LeetCode252.会议室 $ Meeting Rooms
- Linux 中数组的使用
- 【iCore1S 双核心板_ARM】例程十八:SD_IAP_FPGA实验——更新升级FPGA
- (5)修改Linux的基本配置
- python json数据处理
- PAT 乙级 1051 复数乘法 (15) C++版
- WPF Layout 系统概述 MeasureOverride和ArrangeOverride
- 更新k8s镜像版本的三种方式
- springmvc执行流程详细介绍
热门文章
- Java微信公众平台开发(七)--多媒体消息回复之图片回复
- 我的NopCommerce之旅(2): 系统环境及技术分析
- spark常用参数
- Sencha Touch和jQuery Mobile的比较
- 基于H5+ API手机相册图片压缩上传
- 如何使用Git Bash Here,将本地项目传到github上
- COGS 1144. [尼伯龙根之歌] 精灵魔法
- COGS 1786. 韩信点兵
- Nodejs + Jshint自动化静态代码检查
- 数据库_4_SQL介绍