首页 > Web开发 > 详细

AngularJs 中的transclude的理解

时间:2017-05-17 16:38:34      阅读:332      评论:0      收藏:0      [点我收藏+]

Transclude是一个配置, 为了告诉AngularJs去获取当前指令模版内部的所有内容(实际使用ng-transclude), 更多关于怎么创建一个包含其他元素的指令: documentation of directives

下面自定义一个指令用ng-transclude在指令模版中去指定你想插入的内容:

angular.module(‘app‘, [])
  .directive(‘hero‘, function () {
    return {
      restrict: ‘E‘,
      transclude: true,
      scope: { name:‘@‘ },
      template: ‘<div>‘ +
                  ‘<div>{{name}}</div><br>‘ +
                  ‘<div ng-transclude></div>‘ +
                ‘</div>‘
    };
  });

代码使用如下:

<hero name="superman">Stuff inside the custom directive</hero>

页面显示如下:

Superman

Stuff inside the custom directive

完整的例子: 

Index.html

<body ng-app="myApp">
  <div class="AAA">
   <hero name="superman">Stuff inside the custom directive</hero>
</div>
</body>

jscript.js

angular.module(‘myApp‘, []).directive(‘hero‘, function () {
    return {
      restrict: ‘E‘,
      transclude: true,
      scope: { name:‘@‘ },
      template: ‘<div>‘ +
                  ‘<div>{{name}}</div><br>‘ +
                  ‘<div ng-transclude></div>‘ +
                ‘</div>‘
    };
  });

页面: 

技术分享

实现: 

技术分享

AngularJs 中的transclude的理解

原文:http://www.cnblogs.com/ZengYunChun/p/6868040.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!