angularjs中的指令

本文详细介绍了AngularJS中指令与控制器、指令与指令之间的交互方式。通过具体示例讲解了如何利用link函数实现指令间的通信,并展示了如何使用require属性来请求其他控制器。

一、指令与控制器之间的交互:通过指令中的link函数来实现

<body ng-app="myApp" ng-controller="myCtrl">
    <my-print>鼠标进入触发打印</my-print>

    <script>
        angular.module('myApp',[])
        .controller('myCtrl',function($scope){
            $scope.print=function(){
                console.log('我是打印数据....')
            }
        })
        .directive('myPrint',function(){
            return{
                restrict:'E',
                link:function(scope,element,attrs){
                    element.bind('mouseenter',function(){
                        scope.print();
                    })
                }
            }
        })
    </script>
</body>

二、指令与指令间的交互:

<body ng-app="myApp">
    <div>
        <superman strength>动感超人--力量</superman>
    </div>
    <div>
        <superman strength speed>动感超人--力量+敏捷</superman>
    </div>
    <div>
        <superman strength speed light>动感超人--力量+敏捷+发光</superman>
    </div>

    <script>
        var app=angular.module('myApp',[])
        app.directive('superman',function(){
            return{
                restrict:'E',
                scope:{},
                controller:function($scope){
                    $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){
                    element.bind('mouseenter',function(){
                        console.log(scope.abilities);
                    })
                }
            }
        })
        app.directive('strength',function(){
            return{
                require:'^superman',
                link:function(scope,element,attrs,supermanCtrl){
                    supermanCtrl.addStrength();
                }
            }
        })
        app.directive('speed',function(){
            return{
                require:'^superman',
                link:function(scope,element,attrs,supermanCtrl){
                    supermanCtrl.addSpeed();
                }
            }
        })
        app.directive('light',function(){
            return{
                require:'^superman',
                link:function(scope,element,attrs,supermanCtrl){
                    supermanCtrl.addLight();
                }
            }
        })
    </script>
</body>

三、示例说明

1.自定义指令下的link函数有四个参数:scope,element,attrs和ctrl(关联ctrl)

2.require:请求另外的controller,传入当前directive的link 函数中。require需要传入一个directive controller的名称。如果找不到这个名称对应的controller,那么将会抛出一个error。名称可以加入以下前缀:

         ? - 不要抛出异常。这使这个依赖变为一个可选项。

         ^ - 允许查找父元素的controller

3.在上面的示例中<superman></superman>指令中添加的strength speed light其实也是指令,是以属性的方式存在的。

 

转载于:https://www.cnblogs.com/iagw/p/6875875.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值