angularjs中的页面完成事件

时间:2015-09-01 11:01:39

标签: javascript angularjs

我试图在完成模板渲染后在angularJs中获取页面完成事件。

以下是我尝试的代码: -

app.controller('Mycontroller',function($scope,$http,$stateParams,$sce,$timeout){    

var request = $http({       
        url:"/test.php",            
        headers: {'Content-Type' : 'application/x-www-form-urlencoded'}                     
    }); 

    request.success(function(data){                     
        $scope.testhtml = $sce.trustAsHtml(data);               
    });

    $scope.$on('$viewContentLoaded', function(event) {   
            $('#firstcontent').trigger('create'); 
            alert('fullyloaded');       // this alert I am getting before template rendering , (I want this alert after template rendering )
    }); 
});

1 个答案:

答案 0 :(得分:0)

如果您在$timeout内执行代码,则可以确定该模板已经呈现:

request.success(function(data){
    // executed within the currently running digest
    $scope.testhtml = $sce.trustAsHtml(data);

    $timeout(function(){
        // executes after the previously mentioned digest is complete
        // and after the DOM has been rendered
        $('#firstcontent').trigger('create');
        alert('fullyloaded');
    });
});

您可能会发现以下答案另外有用:

  1. https://stackoverflow.com/a/17239084/1356062
  2. https://stackoverflow.com/a/17303759/1356062