Angular中$ viewContentLoaded的奇怪行为

时间:2015-03-26 17:12:10

标签: javascript jquery angularjs angularjs-scope

我正在编写一个 jquery 代码,我需要在加载模板视图并修改DOM后立即执行。

我在控制器中使用$viewContentLoaded 事件来实现此目的。

$scope.$on('$viewContentLoaded', function(){
    //Here your view content is fully loaded !!
    page_content_onresize();  
});

但不幸的是$viewContentLoaded对我没用。

因为我的代码需要在 DOM被修改之后立即在其视图中加载模板后应用,但是当我没有使用该ng视图并且正在加载时,一切正常我的html直接使用而不使用任何模板

我尝试了所有内容,但我没有使用$viewContentLoaded正确运行我的脚本。

所以我把我的代码改成了这样的东西,然后一切都很完美。

$scope.$on('$viewContentLoaded', function(){
    //Here your view content is fully loaded !!
    setTimeout(page_content_onresize, 0);

});

我不明白为什么它在第一种情况下没有工作?

function page_content_onresize(){
    $(".page-content,.content-frame-body,.content-frame-right,.content-frame-left").css("width","").css("height","");

    var content_minus = 0;
    content_minus = ($(".page-container-boxed").length > 0) ? 40 : content_minus;
    content_minus += ($(".page-navigation-top-fixed").length > 0) ? 50 : 0;

    var content = $(".page-content");
    var sidebar = $(".page-sidebar");

    if(content.height() < $(document).height() - content_minus){        
        content.height($(document).height() - content_minus);
    }        

    if(sidebar.height() > content.height()){        
        content.height(sidebar.height());
    }

    if($(window).width() > 1024){

        if($(".page-sidebar").hasClass("scroll")){
            if($("body").hasClass("page-container-boxed")){
                var doc_height = $(document).height() - 40;
            }else{
                var doc_height = $(window).height();
            }
            $(".page-sidebar").height(doc_height);

        }

        if($(".content-frame-body").height() < $(document).height()-162){
            $(".content-frame-body,.content-frame-right,.content-frame-left").height($(document).height()-162);            
        }else{
            $(".content-frame-right,.content-frame-left").height($(".content-frame-body").height());
        }

        $(".content-frame-left").show();
        $(".content-frame-right").show();
    }else{
        $(".content-frame-body").height($(".content-frame").height()-80);

        if($(".page-sidebar").hasClass("scroll"))
               $(".page-sidebar").css("height","");
    }

    if($(window).width() < 1200){
        if($("body").hasClass("page-container-boxed")){
            $("body").removeClass("page-container-boxed").data("boxed","1");
        }
    }else{
        if($("body").data("boxed") === "1"){
            $("body").addClass("page-container-boxed").data("boxed","");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

加载模板后将触发

$viewContentLoaded事件。 它不能保证在页面上呈现加载的模板。

来自angular.js的

代码段

var clone = $transclude(newScope, function(clone) {
  $animate.enter(clone, null, currentElement || $element).then(function onNgViewEnter() {
    if (angular.isDefined(autoScrollExp)
      && (!autoScrollExp || scope.$eval(autoScrollExp))) {
      $anchorScroll();
    }
  });
  cleanupLastView();
});

currentElement = clone;
currentScope = current.scope = newScope;
currentScope.$emit('$viewContentLoaded');
currentScope.$eval(onloadExp);
来自ngAnimate.js的

代码段

enter: function(element, parentElement, afterElement, options) {
  options = parseAnimateOptions(options);
  element = angular.element(element);
  parentElement = prepareElement(parentElement);
  afterElement = prepareElement(afterElement);

  classBasedAnimationsBlocked(element, true);
  $delegate.enter(element, parentElement, afterElement);
  return runAnimationPostDigest(function(done) {
    return performAnimation('enter', 'ng-enter', stripCommentsFromElement(element), parentElement, afterElement, noop, options, done);
  });
}

$ animate.enter不同步。

因此,首先发出$viewContentLoaded,然后将内容呈现到页面上。

相关问题