简体   繁体   English

AngularJS在部分内部循环中的编译内部呈现模板

[英]AngularJS to render a template inside a compile inside a loop inside a partial

I have a partial, which has a directive which renders a bunch of things in a loop, using compiled HMTL-as-a-string inside the directive. 我有一个局部指令,该指令有一个指令,该指令在指令内部使用编译后的HMTL-as-a-string在一个循环中渲染一堆东西。 This HTML-as-a-string itself contains an ng-include , which doesn't render out. 此HTML字符串本身包含ng-include ,但不会呈现出来。

Please see my jsfiddle example 请参阅我的jsfiddle示例

Basically, this doesn't include template2.html : 基本上,这不包括template2.html

element.html('<span>The name is ' + scope.content.name + '!</span><div ng-include src="template2.html"></div><br>');

Any pointers would be much appreciated. 任何指针将不胜感激。

Thanks! 谢谢!

WORKING DEMO 工作演示

just needed to write as 只是需要写为

src=" \'template2.html\'"


var linker = function(scope, element, attrs) {
    element.html('<span>The name is ' + scope.content.name + '!</span><div ng-include src=" \'template2.html\'"></div><br>');
    $compile(element.contents())(scope);
};

more info in DOCS 更多的DOCS信息

Vinod's answer above (replace src="template2.html" with src="\\'template2.html\\'" ) is correct, though I would recommend using an actual template instead of manually compiling the template yourself inside the link function. Vinod的以上回答(用src="\\'template2.html\\'"代替src="template2.html" )是正确的,尽管我建议使用实际模板,而不是自己在链接函数中手动编译模板。 In your example, you aren't actually receiving the benefit of the two way binding. 在您的示例中,您实际上没有获得双向绑定的好处。 You are just taking the html output of the compile function, and it will never update if the underlying data changes. 您只是获取compile函数的html输出,并且如果基础数据发生更改,它将永远不会更新。 Here is your example modified to show the bindings (and Vinod's template fix): 这是修改示例以显示绑定(和Vinod的模板修复):

http://jsfiddle.net/kf3vZ/5/ http://jsfiddle.net/kf3vZ/5/

Notice how if you change the value of any of the checkboxes, the value in the directives do not change. 请注意,如果您更改任何复选框的值,则伪指令中的值不会改变。

Now here is a version using the template argument to the directive: 现在是使用指令的template参数的版本:

http://jsfiddle.net/kf3vZ/7/ http://jsfiddle.net/kf3vZ/7/

Now if you change the text fields, the directive values will change also. 现在,如果您更改文本字段,则指令值也将更改。

Another note, since you are already using the script tags for your templates, you could replace template in your directive with templateUrl and provide the id of the script template. 另外要注意,因为你已经在使用script标签为你的模板,你可以替换template与您的指令templateUrl并提供id脚本模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM