directive之间交互--动感超人案例

<!DOCTYPE HTML>
<html ng-app="MyModule">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>directive之间交互--动感超人案例</title>
	<link rel="stylesheet" href="CSS/bootstrap.css">

    <script src="angular.min.js"></script>

</head>

<body>
<!--  指令间的交互
    -->
<div class="row">
    <div class="col-md-3">
		<superman  strength>动感超人--力量</superman>
	</div>
</div>

<div class="row">
    <div class="col-md-3">
		<superman strength speed>动感超人--力量 速度</superman>
	</div>
</div>

<div class="row">
	<div class="col-md-3">
		<superman strength speed light>动感超人--力量 速度 发光</superman>
	</div>
</div>



</body>
<script>
	var myModule = angular.module("MyModule",[]);

	myModule.directive("superman",function(){
	    return {
	        scope: {},//独立作用域
	        restrict : "AE",
			controller:function($scope){    //暴露一组public的方法给外部调用
	            $scope.abilities = [];
	            this.addStrength = function(){
	                $scope.abilities.push("strength");
				};
	            this.addSpeed = function(){
	                $scope.abilities.push("speed");
				};
	            this.addLight = function () {
					$scope.abilities.push("light");
                };
			},
			link : function(scope,element,attrs){  //处理指令内部的事件  操作DOM元素
			    element.addClass('btn btn-primary');
	            element.bind("mouseenter",function(){
                console.log(scope.abilities);

	            });
			}
		}
	});
	myModule.directive("strength",function(){
	    return {
	        require : '^superman',         //require 用于引入依赖
			link : function (scope, element,attrs, supermanCtrl) {
				supermanCtrl.addStrength();   // 注入supermanCtrl 调用superman指令中通过controller暴露出来的方法
            }

		}
	});
	myModule.directive("speed",function(){
	    return {
	        require : '^superman',
			link: function (scope,element,attrs,supermanCtrl) {
                supermanCtrl.addSpeed();
            }
		}
	});
	myModule.directive("light",function(){
	    return {
	        require : '^superman',
			link: function (scope,element,attrs,supermanCtrl) {
                supermanCtrl.addLight();
            }
		}
	});

</script>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值