AngularJS document.ready doesn't work when using ng-view

前端 未结 3 1918
粉色の甜心
粉色の甜心 2020-12-13 20:14

I have a problem with document.ready in angularJS when navigating between several routes in my app. It only works when I use ctrl+f5 (page reload); it seems navigating betwe

3条回答
  •  北海茫月
    2020-12-13 20:29

    Expanding on @davekr's answer, I found I need to add a $timeout to ensure the digest had complete and the html elements were available to query:

    function SomeController($scope) {
        $scope.$on('$viewContentLoaded', function() {
            $timeout(function(){
                //Do your stuff
            });
        });
    }
    

    I tried many other events and this was the only way that worked reliably.

提交回复
热议问题