简体   繁体   English

侦听指令以完成在 DOM 中的加载 - AngularJS

[英]Listen for directive to have completed loading in DOM - AngularJS

I would like to provide a loading indication as long as the directive has not finished loading all it's child elements on the page.只要指令尚未完成加载页面上的所有子元素,我就想提供加载指示。

This portion of my app has tabs which when clicked will load the tab content.我的应用程序的这一部分具有选项卡,单击时将加载选项卡内容。 The problem I have is that some tabs content takes a long time to render.我遇到的问题是某些选项卡内容需要很长时间才能呈现。

I assumed it was my HTTP request which was causing a delay in the rendering and resorted to using custom promises, but after implementing that I found it was actually the DOM rendering of the directive that was taking so long.我认为是我的 HTTP 请求导致了渲染延迟并使用自定义承诺,但在实现之后我发现它实际上是指令的 DOM 渲染花费了这么长时间。

What would be the correct event to listen to in the directive if I want to display a loading indicator before all the content is rendered within this directive?如果我想在此指令中呈现所有内容之前显示加载指示器,那么在指令中侦听的正确事件是什么? As far as I know, $viewContentLoaded is only used for the ngView directive, but this directive is a sub-directive of ngView so it does not apply (i think).据我所知, $viewContentLoaded仅用于ngView指令,但该指令是 ngView 的子指令,因此它不适用(我认为)。

The code is pretty basic:代码非常基本:

Controller:控制器:

angular.module('app', [])

.controller('MyController', ['$scope', 'Service', function($scope, Service) {
    
    $scope.get_data = function(user) {
        Service.getsomething(user).then(function(response) {
            $scope.data = response.data;
        },
        function(error) {
            console.log(error);
        });
    };
    
}])
.directive('tab', function() {
        return {
            restrict: 'E',
            templateUrl: 'templates/tab.html'
        };
    })

HTML: HTML:

<div role="tabpanel">
    <ul class="nav nav-pills col-lg-9 col-lg-offset-3" role="tablist">
        <li toggle-tabs class="active">
             <a href="#apanel">Panel</a>
        </li>
    </ul>

    <div class="tab-content col-lg-12">

        <tab></tab> //within here there are many forms

    </div>
</div>
angular.module('app').directive('tab', function($timeout, $rootScope) {
  return {
      restrict: 'E',
      templateUrl: 'templates/tab.html',
      link: function (scope, element, attrs, ctrl) {
        $timeout(function () {
          $rootScope.$emit('directive-dom-is-most-likely-there');
        });
      }
  };
});

You can't be sure that DOM is there when link runs, it depends on the implementation of nested directives (eg ng-repeat is known to be a late bloomer).您不能确定link运行时 DOM 是否存在,这取决于嵌套指令的实现(例如ng-repeat被认为是大器晚成的)。 Either you can't be sure that none of them has async stuff that needs some time to be completed, but $timeout with zero delay is ok for cosmetic things.要么你不能确定它们都没有需要一些时间才能完成的异步内容,但是零延迟的$timeout对于装饰性的东西来说是可以的。

It depends on the context, it is not necessary to pollute $rootScope when you can talk to parent directive with require .这取决于上下文,当您可以使用require与父指令交谈时,没有必要污染$rootScope

Keep in mind that it is more straightforward to make the directive self-contained with its own loading indicator instead of the global one.请记住,使用自己的加载指示器而不是全局指示器使指令自包含更直接。

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

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