Finished Loading of ng-include in angular js

后端 未结 6 535
春和景丽
春和景丽 2020-12-13 02:28

What is the best way to detect the end of html loading by ng–include? I want to write some code that runs when it has finished loading.

6条回答
  •  忘掉有多难
    2020-12-13 02:51

    If I have to wait for the element to be present, I wrap a $timeout with $includeContentLoaded:

    var selector = '#foo';
    $rootScope.$on('$includeContentLoaded', function(event, templateName){
      $timeout(() => {
        const $el = angular.element(selector);
        if ($el.length) {
          // Do stuff with element.
        }
      });
    });
    

    The timeout gives it time load properly, specially if the ng-include contains a directive that takes a few milliseconds to render.

提交回复
热议问题