Angularjs templateUrl无法绑定纳克重复中的属性 [英] Angularjs templateUrl fails to bind attributes inside ng-repeat

查看:156
本文介绍了Angularjs templateUrl无法绑定纳克重复中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用指令来显示HTML片段。

I'm using directive to display html snippets.

和templateUrl指令里面,
要能包含片段为html文件。

And templateUrl inside the directive, to be able to include snippets as html file.

该指令不起作用,如果我尝试打电话
一个内置NG重复指令内
({{剪断}}传递,而不做替补):

The directive does not work, if I try to call inside a builtin ng-repeat directive ({{snip}} is passed as is, without substitute):

div ng-repeat="snip in ['snippet1.html','snippet2.html']">
  <my-template snippet="{{snip}}"></my-template>
</div>

有关参考,这里是指令:

For reference, here is the directive:

app.directive("myTemplate", function() {
return {
    restrict: 'EA',
    replace: true,
    scope: { snippet: '@'},
    templateUrl: function(elem, attrs) {
      console.log('We try to load the following snippet:' + attrs.snippet);
      return attrs.snippet;
    }
  };
});

也是一个 plunker演示

任何指针是多少AP preciated。
(该指令是比较复杂的在我的code,
我试图得到一个最小的实施例,其中,问题是可重复的。)

Any pointer is much appreciated. (the directive is more complicated in my code, I tried to get a minimal example, where the issue is reproducible.)

推荐答案

ATTRS 参数为 templateUrl 不插指令执行过程中。您可以使用下面的方式来实现这一目标。

attrs param for templateUrl is not interpolated during directive execution. You may use the following way to achieve this

app.directive("myTemplate", function() {
  return {
    restrict: 'EA',
    replace: false,
    scope: { snippet: '@'},
    template: '<div ng-include="snippet"></div>'
  };
}); 

演示: http://plnkr.co/edit/2ofO6m45Apmq7kbYWJBG?p=preVIEW

这篇关于Angularjs templateUrl无法绑定纳克重复中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆