【问题标题】:AngularJS - how do you know when angular has finished processing a page?AngularJS - 你怎么知道 Angular 何时完成处理页面?
【发布时间】:2013-11-14 15:39:52
【问题描述】:

如何知道给定页面上的所有插值和处理何时完成?

            compile: function (tElement, tAttrs) {
                return function (scope, element, attrs) {
                    element.html(tpl);
                    $compile(element.contents())(scope);
                };
            },

这不是同步的。如果有 {{stuff}} 和 ng-repeat="..." 等...在链接函数返回时,它们并不能保证全部完成。

我需要一种方法来了解页面何时呈现并且没有更多指令要处理,以便我可以使用#hashes 导航到页面上通过角度创建的元素。 (使用 $anchorScroll)

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    不妨试试这个:

    $scope.$on('$viewContentLoaded', function() {
      // ....
    });
    

    【讨论】:

      【解决方案2】:

      由于这个非常具体的原因,Angular 中有一个指令; ngCloak。

      ngCloak 指令用于阻止 Angular html 模板 浏览器以原始(未编译)的形式简要显示 应用程序加载时的表单。使用该指令来避免 html模板显示引起的不良闪烁效果。

      文档@https://docs.angularjs.org/api/ng/directive/ngCloak

      Quora 上的类似问题; 如何在 ng-repeat 完成处理之前使用 AngularJS 隐藏 div? @https://www.quora.com/How-do-I-hide-a-div-with-AngularJS-until-ng-repeat-has-finished-processing

      这是另一种方法,但我更喜欢使用角度指令。

      <li ng-repeat="opt in menuItems" my-custom-repeat-listener> </li>
      

      然后在指令上有点像

      if(scope.$last) { /*fire event or etc to show the list items*/ }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-18
        • 2017-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多