angularJS1笔记-(12)-自定义指令(compile/link)

Posted 有时你唱起歌 有时你沉默 有时你望着天空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了angularJS1笔记-(12)-自定义指令(compile/link)相关的知识,希望对你有一定的参考价值。

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<div ng-app="myApp">
    <div  ng-controller="firstController">
        <div ng-repeat="user in users" custom-tags><a href="www.baidu.com">baidu</a></div>
    </div>
</div>

<script type="text/javascript" src="../../vendor/angular/angularJs.js"></script>
<script type="text/javascript" src="app/index.js"></script>

<script>
</script>

</body>
</html>

  index.js:

var myApp = angular.module(\'myApp\', [])
    .directive(\'customTags\',function () {
        return{
            restrict:"ECAM",
            template:"<div>aaa<span ng-transclude></span></div>",
            replace:true,
            transclude:true,
            compile:function (tElement,tAttrs,transclude) {
                console.log(tElement);
                console.log(tAttrs);
                console.log(\'customTags compile\');
                console.log("你好,我是编译阶段的方法"); //在编译阶段只执行一次
                return function () {
                    console.log(\'compile return function\')
                }
            }
        }
    })
    .controller(\'firstController\',[\'$scope\',function ($scope) {
        $scope.users = [
            {
                id:10,
                name:\'张三\'
            },
            {
                id:20,
                name:\'李四\'
            }
        ];
    }]);

  运行结果:

 

以上是关于angularJS1笔记-(12)-自定义指令(compile/link)的主要内容,如果未能解决你的问题,请参考以下文章

angularJS1笔记-(10)-自定义指令(templateUrl属性)

AngularJS1.X学习笔记9-自定义指令(中)

AngularJS1.X学习笔记10-自定义指令(下)

angularJS1笔记--自定义指令(restrict/template/replace)

angularJS1笔记--内置指令

angularjs1 制作自定义轮播图(汉字导航)