angularjs指令 - element.html()打破了transclusion

时间:2013-05-24 18:13:41

标签: angularjs angularjs-directive

Reference plunker

如果我在链接函数中调用element.html(),则不再编译已转换的元素。这真的说明了一切,并且说明了这一点,但这里是代码:

指令:

app.directive('myDirective', function() {
  var compile, config, link;

  link = function(scope, element, attr) {
      element.html();
  };

  compile = function() {
    return link;
  };

  config = {
    transclude: true,
    template: '<div ng-transclude></div>',
    compile: compile,
    scope: {}
  };

  return config;
});

HTML:

在下文中,输出只是未编译的{{foo}}

  <div my-directive>
    {{foo}}
  </div>  

这是一个错误还是我错过了一些基本的东西?

N.B。如果在element.html()函数中调用compile,则会出现问题。

Angular v1.0.6,jQuery v1.9.1

1 个答案:

答案 0 :(得分:0)

代码似乎没有任何问题:这是

JSFiddle

 <div my-directive>
   {{foo}}
 </div>  

解析为:

<div my-directive>
 test    
</div>