javascript - AngularJS 中的第一个、ng-include 或 ng-repeat 是什么?

标签 javascript angularjs

如果我有一些包含 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/

相关文章:

java - Servlet 返回 "到 AngularJS2 调用

javascript - 具有悬停效果的响应图像

javascript - Sencha Touch 2,来自远程代理的每个存储负载的 CPU 使用率增加

javascript - 带有大写字母的 Angular-slick 属性

javascript - 如何在 Angular js APP 的 config.js 中正确注入(inject) adalprovier

javascript - angularjs 的 Bootstrap datepicker 指令

javascript - 如何使用 _.reduce(和 _.each)从 Underscore.js 重写 _.every/_.all

javascript - 横幅 javascript 变得更快

javascript - Angular 2 - 停止点击事件传播

angularjs - 为什么在注释中使用 AngularJS 指令