Angular的自定义指令及其实例

前端之家收集整理的这篇文章主要介绍了Angular的自定义指令及其实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面我们来看看如何使用directive自定义指令。

先看一个例子:

<body>
    <div my-hello></div>
</body>

<script type="text/javascript">
@H_403_13@var m1 = angular.module('myApp'@H_403_13@,[]);
m1.directive('myHello',@H_403_13@function@H_403_13@(){
    @H_403_13@return@H_403_13@ {
        restrict : 'A'@H_403_13@,replace : @H_403_13@true@H_403_13@,template : '<div>hello angular</div>'@H_403_13@
    };
});
</script>

1:我们定义了一个my-hello的指令。

2:使用directive完善这个指令,返回一个对象。有三个值:

  a) :restrict共四个值:E:标签指令,C:class指令,M:注释指令,A:属性指令

    如何使用 ?

    

  b):replace是否替换(M注释必须为true才能解析)看图:

    true:

    false:

  c):template内容,除此之外还有templateUrl,指定一个html模板文件

下面再举个例子:

<div ng-controller=@H_403_13@"@H_403_13@Aaa@H_403_13@">
    <div my-tab my-id=@H_403_13@"@H_403_13@div1@H_403_13@" my-name=@H_403_13@"@H_403_13@name@H_403_13@" my-fn=@H_403_13@"@H_403_13@show(num)@H_403_13@" @H_403_13@class=@H_403_13@"@H_403_13@J-tab@H_403_13@"></div>
    <div my-tab my-id=@H_403_13@"@H_403_13@div2@H_403_13@" my-name=@H_403_13@"@H_403_13@name@H_403_13@" my-fn=@H_403_13@"@H_403_13@show(num)@H_403_13@" @H_403_13@class=@H_403_13@"@H_403_13@J-tab@H_403_13@"></div>
</div>
<script type=@H_403_13@"@H_403_13@text/javascript@H_403_13@">

@H_403_13@var m1 = angular.module(@H_403_13@'@H_403_13@myApp@H_403_13@'@H_403_13@,[]);

m1.controller(@H_403_13@'@H_403_13@Aaa@H_403_13@',[@H_403_13@'@H_403_13@$scope</span><span style="margin:0px; padding:0px; color:rgb(128,0); line-height:1.5!important">'</span><span style="margin:0px; padding:0px; line-height:1.5!important">,function($scope){
    $scope.name = @H_403_13@'@H_403_13@xiecg@H_403_13@'@H_403_13@;
    $scope.age = @H_403_13@18@H_403_13@;
    $scope.show =@H_403_13@ function(num){
        console.log(num);
    };
}]);


m1.directive(@H_403_13@'@H_403_13@myTab@H_403_13@'@H_403_13@,function(){
    @H_403_13@return@H_403_13@ {
        restrict : @H_403_13@'@H_403_13@ECMA@H_403_13@'@H_403_13@,replace : @H_403_13@true,@H_403_13@//@H_403_13@替换的方式插入内容@H_403_13@//@H_403_13@绑定策略
@H_403_13@        scope : {
            myId : @H_403_13@'@H_403_13@@@H_403_13@',@H_403_13@//@H_403_13@解析普通字符串
            myName : @H_403_13@'@H_403_13@=@H_403_13@',@H_403_13@//@H_403_13@解析数据
            myFn : @H_403_13@'@H_403_13@&@H_403_13@'        @H_403_13@//@H_403_13@函数
@H_403_13@        },controller : [@H_403_13@'@H_403_13@$scope</span><span style="margin:0px; padding:0px; color:rgb(128,function($scope){
            @H_403_13@//@H_403_13@共享数据存放在这里
            $scope.name = @H_403_13@'@H_403_13@this is a xiecg@H_403_13@'@H_403_13@;
        }],template : @H_403_13@'@H_403_13@<div id="{{myId}}">\
                    <input type=@H_403_13@"@H_403_13@button@H_403_13@" value=@H_403_13@"@H_403_13@1@H_403_13@" @H_403_13@class=@H_403_13@"@H_403_13@active@H_403_13@" ng-click=@H_403_13@"@H_403_13@myFn({num:456})@H_403_13@">@H_403_13@\
                    <input type=@H_403_13@"@H_403_13@button@H_403_13@" value=@H_403_13@"@H_403_13@2@H_403_13@">@H_403_13@\
                    <input type=@H_403_13@"@H_403_13@button@H_403_13@" value=@H_403_13@"@H_403_13@3@H_403_13@">@H_403_13@\
                    <div style=@H_403_13@"@H_403_13@display:block;@H_403_13@">{{myName}}</div>@H_403_13@\
                    <div>@H_403_13@2222</div>@H_403_13@\
                    <div>@H_403_13@3333</div>@H_403_13@\
                </div>@H_403_13@'
@H_403_13@    };
});

</script>

1:scope默认是false,为true表示独立作用域。

2:scope给予一个对象时,表示执行绑定策略,在template上调用这些数据。

  a):我们在DOM元素上my-id,我们使用@符号,表示解析普通字符串,说白了就是你写什麽就是什麽。

  b):使用=符号,表示解析数据。

  c):使用&符号,表示这绑定一个函数

3:controller,表示绑定指令内部使用的数据。

好,下面来继续完善这个tab切换的例子!

完整代码

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Tab选项卡实例</title>
<style type="text/css">@H_403_13@
    .J-tab .active{background-@H_403_13@color:#03A9F4;}
    .J-@H_403_13@tab div{display:none;}
</style>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/angular.min.js"></script>
</head>
<body>

<div ng-controller="Aaa">
    <my-tab my-id="div1" my-data="sports" class="J-tab"></my-tab>
    <my-tab my-id="div2" my-data="time" class="J-tab"></my-tab>
</div>

<script type="text/javascript">

@H_403_13@var m1 = angular.module('myApp'@H_403_13@,[]);
m1.controller('Aaa',['$scope',<span style="margin:0px; padding:0px; color:rgb(0,255); line-height:1.5!important">function</span><span style="margin:0px; padding:0px; line-height:1.5!important">($scope){
    $scope.sports =@H_403_13@ [
        {title : '篮球',content : '111111111'@H_403_13@},{title : '足球',content : '222222222'@H_403_13@},{title : '排球',content : '333333333'@H_403_13@}
    ];
    $scope.time =@H_403_13@ [
        {title : '上午',content : '444444444'@H_403_13@},{title : '中午',content : '555555555'@H_403_13@}
    ];
}]);

m1.directive('myTab',@H_403_13@function@H_403_13@(){
    @H_403_13@return@H_403_13@ {
        restrict : 'E'@H_403_13@,scope : {
            myId : '@'@H_403_13@,myData : '='@H_403_13@
        },controller : ['$scope',255); line-height:1.5!important">function</span><span style="margin:0px; padding:0px; line-height:1.5!important">($scope){
            $scope.name = 'this is a xiecg'@H_403_13@;
        }],template : '@H_403_13@<div id="{{myId}}">\
                    <input ng-repeat="data in myData" type="button" ng-value="data.title" ng-class="{active:$first}">\
                    <div ng-repeat="data in myData" ng-style="{display:$first?\'block\':\'none\'}">{{data.content}}</div>\
                </div>'@H_403_13@,link : @H_403_13@function@H_403_13@(scope,element,attr){
            element.on('click','input',@H_403_13@function@H_403_13@(){
                @H_403_13@var self = $(@H_403_13@this),i =@H_403_13@ self.index();
                self.addClass('active').siblings('input').removeClass('active'@H_403_13@);
                self.siblings('div').eq(i).show().siblings('div'@H_403_13@).hide();
            });
        }
    };
});


</script>
</body>
</html>

link属性,表示当directive被angular编译后,执行该方法。这个方法接受三个参数,

a):scope表示controller下面的数据。

b):element表示当前的DOM元素。

c):attr表示这个DOM元素上的自定义属性

补充:

在实际的开发过程中我们往往需要嵌套各种组件和指令。下面来介绍directive中的transclude和require。

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>自定义指令间的互相交互</title>
<script type="text/javascript" src="js/angular.min.js"></script>
</head>
<body>

<div>
    <hello>
        <hi></hi>
    </hello>
</div>

<script type="text/javascript">

@H_403_13@var m1 = angular.module('myApp'@H_403_13@,[]);

m1.directive('hello',transclude : @H_403_13@true,@H_403_13@//@H_403_13@允许自定义指令的嵌套,通过ng-transclude指定嵌套的范围
        controller : @H_403_13@function@H_403_13@($scope){
            $scope.name = 'xiecg'@H_403_13@;
            @H_403_13@this.name = 'xiecg';    @H_403_13@//@H_403_13@使用this共享给其他指令
@H_403_13@        },template : '<div>hello angular <h1 ng-transclude></h1></div>'@H_403_13@
    };
});

m1.directive('hi',require : '^hello',@H_403_13@//@H_403_13@hello指令属性hi指令的父级,需要用^符号指定。如果无法指定,使用?容错处理。
        template : '<span>hi angular {{name}}</span>'@H_403_13@,attr,reController){
            console.log(reController);    @H_403_13@//@H_403_13@得到父级hello指令中共享出来的数据
@H_403_13@        }
    };
});

</script>
</body>
</html>

转载地址: http://www.cnblogs.com/xiaoxie53/p/5058198.html

猜你在找的Angularjs相关文章