angularjs 当transclude:true or elemet时如何在link函数中取得要替代模板中ng-transclude的那部分内容

来源:互联网 发布:软件开发面试项目 编辑:程序博客网 时间:2024/05/15 23:52
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="bootstrap.min.css">
        <script src="jquery.min.js"></script>
        <script src="angular.js"></script>
        <script src="bootstrap.min.js"></script>
        <script type="text/javascript">
            var app = angular.module('myapp', []);
            app.directive('cust', function(){
              return {
                restrict: 'EA',
                transclude: true,
                replace: true,
                link: function(scope, elem, attrs, ctrl, transclude) {
                    transclude(scope, function(clone) {
                        console.log(clone);
                    });
                },
                template:"<span ng-transclude></span>"
              }
            });   
        </script>
        <style type="text/css">
        </style>
    </head>
    <body ng-app="myapp">
       <cust><div>hello</div></cust>
    </body>

</html>

可以看到link函数的第五个参数是一个函数,它的第二个参数也是一个函数,其中clone就是那部分要被嵌入template中的内容<div>hello</div>.


0 0
原创粉丝点击