如果我有一些包含 ng-repeat 的模板并将其包含在 ng-include 中,实际上会发生什么? 将包含已完成 ng-repeat 的模板,还是未完成 ng-repeat 的模板(包含 ng-repeat 后将完成)?
最佳答案
ngInclude 指令首先将从模板返回的 html 附加到 html DOM。 然后它编译其内容。
正如您在 ngInclude 的源代码中看到的
// This directive is called during the $transclude call of the first `ngInclude` directive.
// It will replace and compile the content of the element with the loaded template.
// We need this directive so that the element content is already filled when
// the link function of another directive on the same element as ngInclude
// is called.
var ngIncludeFillContentDirective = ['$compile',
function($compile) {
return {
restrict: 'ECA',
priority: -400,
require: 'ngInclude',
link: function(scope, $element, $attr, ctrl) {
if (/SVG/.test($element[0].toString())) {
// WebKit: https://bugs.webkit.org/show_bug.cgi?id=135698 --- SVG elements do not
// support innerHTML, so detect this here and try to generate the contents
// specially.
$element.empty();
$compile(jqLiteBuildFragment(ctrl.template, document).childNodes)(scope,
function namespaceAdaptedClone(clone) {
$element.append(clone);
}, {futureParentElement: $element});
return;
}
$element.html(ctrl.template);
$compile($element.contents())(scope);
}
};
}];
查看行
$element.html(ctrl.template);
$compile($element.contents())(scope);
这意味着它首先将其添加到 DOM,然后编译它。
在底线中,您将其描述为
It's a template without completed ng-repeat (and after include ng-repeat will be completed)
关于javascript - AngularJS 中的第一个、ng-include 或 ng-repeat 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33946577/