angularjs 自定义指令 directive

简介:
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script>
//m1是一个模块对象,下面有run,directive,controller,filter等
var m1 = angular.module('myApp',[]);
m1.directive('myHello',function(){//directive自定义指令,myHello是自定义指令的名字,函数是回调,
    return {
        restrict : 'AECM',   //指令的类型:E表示标签指令即可以写<my-hello></my-hello>,A表示属性指令即写成<p my-hello></p>,C表示class形式写成<p class="hello"></p>,M表示注释指令写成<!-- directive:hello -->。区分大小写,而且是可以组合使用的。
        replace : true,       //替换<my-hello></my-hello>,<p my-hello></p>,<p class="hello"></p>,<!-- directive:hello -->
        template : '<div>hello angular</div>'
    };
});

</script>
</head>

<body>
<my-hello></my-hello>
<p my-hello></p>
<p class="hello"></p>   // 容易与样式class搞混
<!-- directive:hello -->  //容易与注释搞混


</div>
</body>
</html>
复制代码
复制代码
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#div1 div{ width:200px; height:200px; border:1px red solid; display:none;}
#div1 input.active{ background:red;}
</style>
<script src="angular.min.js"></script>
<script>

var m1 = angular.module('myApp',[]);
m1.directive('myTab',function(){
    return {
        restrict : 'E',   
        replace : true,
        /*template : '<div id="div1">\
                    <input class="active" type="button" value="1">\
                    <input type="button" value="2">\
                    <input type="button" value="3">\
                    <div style="display:block">11111111</div>\
                    <div>22222222</div>\
                    <div>33333333</div>\
                </div>'*/
        templateUrl : 'temp2.html'
    };
});

</script>
</head>

<body>
<my-tab></my-tab>
<!--<div id="div1">
    <input class="active" type="button" value="1">
    <input type="button" value="2">
    <input type="button" value="3">
    <div style="display:block">11111111</div>
    <div>22222222</div>
    <div>33333333</div>
</div>-->
</body>
</html>





temp2.html:
<div id="{{myId}}">
    <input class="active" type="button" value="1" ng-click="myFn({num:456})">
    <input type="button" value="2">
    <input type="button" value="3">
    <div style="display:block">{{name}}</div>
    <div>22222222</div>
    <div>33333333</div>
</div>
复制代码
复制代码
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#div1 div,#div2 div{ width:200px; height:200px; border:1px red solid; display:none;}
#div1 input.active , #div2 input.active{ background:red;}
</style>
<script src="angular.min.js"></script>
<script>

var m1 = angular.module('myApp',[]);
m1.directive('myTab',function(){
    return {
        restrict : 'E',   
        replace : true,
        //scope : true,  //true表示不同的my-tab标签作用域独立。
        scope : {  //隔离作用域,
            myId : '@',
            myName : '=',
            myFn : '&'
        },
        controller : ['$scope',function($scope){
            $scope.name = 'miaov';
        }],
        templateUrl : 'temp2.html'
    };
});

m1.controller('Aaa',['$scope',function($scope){
    
    $scope.name = 'hello';
    $scope.show = function(n){
        alert(n);
    };
    
}]);


</script>
</head>

<body ng-controller="Aaa">
<my-tab my-id="div1" my-name="name" my-fn="show(num)"></my-tab>
<my-tab my-id="div2" my-name="name" my-fn="show(num)"></my-tab>
</body>
</html>
复制代码
复制代码
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#div1 div,#div2 div{ width:200px; height:200px; border:1px red solid; display:none;}
#div1 input.active , #div2 input.active{ background:red;}
</style>
<script src="jquery-1.11.1.js"></script>
<script src="angular.min.js"></script>
<script>

var m1 = angular.module('myApp',[]);
m1.directive('myTab',function(){
    return {
        restrict : 'E',   
        replace : true,
        scope : {   
            myId : '@',
            myData : '='
        },
        controller : ['$scope',function($scope){
            $scope.name = 'miaov';
        }],
        templateUrl : 'temp3.html',
        link : function(scope,element,attr){  //自定义指令的dom操作
            //console.log(scope.name);
            //console.log(element);
            //console.log(attr.myId);
            element.delegate('input','click',function(){
                $(this).attr('class','active').siblings('input').attr('class','');
                $(this).siblings('div').eq( $(this).index() ).css('display','block').siblings('div').css('display','none');
            });
        }
    };
});

m1.controller('Aaa',['$scope',function($scope){
    
    $scope.data1 = [
        {title:'数学',content:'111111111'},
        {title:'语文',content:'222222222'},
        {title:'英语',content:'333333333'}
    ];
    $scope.data2 = [
        {title:'物理',content:'444444444'},
        {title:'化学',content:'555555555'}
    ];
    
}]);


</script>
</head>

<body ng-controller="Aaa">
<my-tab my-id="div1" my-data="data1"></my-tab>
<my-tab my-id="div2" my-data="data2"></my-tab>
</body>
</html>




temp3.html:
<div id="{{myId}}">
    <!--<input class="active" type="button" value="1">
    <input type="button" value="2">
    <input type="button" value="3">
    <div style="display:block">11111111</div>
    <div>22222222</div>
    <div>33333333</div>-->
    <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>
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5741773.html,如需转载请自行联系原作者

相关文章
|
9月前
|
JavaScript
AngularJS中的自定义指令:创建与使用技术详解
【4月更文挑战第27天】本文详细介绍了AngularJS中自定义指令的创建与使用。通过定义指令工厂函数并注册到模块中,可以创建自定义指令,如示例中的`myCustomDirective`。指令的属性(如`restrict`、`template`、`replace`)和方法(如`link`、`scope`)可定制其行为。在HTML中使用`restrict`指定的方式(如元素、属性等)来插入指令。遵循命名规范,避免直接DOM操作,使用隔离作用域和关注重用性与扩展性,能有效提升代码质量。自定义指令是AngularJS强大功能之一,有助于实现复杂DOM操作和组件复用。
|
前端开发 JavaScript 开发者
AngularJS 和 React区别
@[TOC](目录) AngularJS 和 React 是两个目前最为流行的前端框架之一。它们有一些共同点,例如都是基于 JavaScript 的开源框架,都能够帮助开发者构建复杂的单页面应用程序等。但也存在一些不同点,如下所述: # 1. 背景: AngularJS 由 Google 的前雇员 Misko Hevery 开发,并于 2010 年首次发布。它是一个基于 JavaScript 的前端框架,旨在简化应用程序的开发过程。React 由 Facebook 的前雇员 Mark Zuckerberg 开发,并于 2013 年首次发布。它是一个基于 JavaScript 的库,可以用于
167 0
|
JavaScript
Angular1.x的自定义指令directive参数配置详细说明
Angular1.x的自定义指令directive参数配置详细说明
|
Web App开发 JavaScript 前端开发
|
数据安全/隐私保护
|
JavaScript 前端开发 开发者