搜索
您的当前位置:首页正文

AngularJS ——自定义指令deme

来源:二三娱乐
<html>

    <head>
        <meta charset="utf-8">
        <title>指令 自定义指令</title>
        <script 
    </head>

    <body ng-app='myApp' >

        <chaoren CanFly>aaa</chaoren>
    
        <script> 
            var mainApp = angular.module("myApp", []);
            
            mainApp.directive("chaoren", function() {
                return {
                    scope : {}, 
                    restrict: "AE",
                    template: "<h1>我是超人</h1>",
                    controller: function($scope){
                        $scope.list = [];
                        this.addCanFly = function(){
                            $scope.list.push("fly");
                            
                        }
                    },
                    link:function(scope,ele,attrs){
                        ele.bind("mouseenter",function(){ 
                            console.log(scope.list);
                            alert("我会" +  scope.list[0]);
                        });
                    }
                } 
            }); 
            mainApp.directive("canfly",function(){ //这里canfly一定要用小写 
                return{
                    require:'^chaoren',
                    link: function(scope,ele,attr,superctr){
                        superctr.addCanFly();
                    }
                }
            });
        
        </script> 

    </body>

</html>
Top