21

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 between pages does not change state of the document to ready.

Controller

  angular.element(document).ready(function() {
    window.scrollTo(0,90);
});

Main html file

<!DOCTYPE html >
<html ng-app="myApp">
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title></title>
    </head>
    <body>
        <div class="container">
            <div ng-view></div>
        </div>
    </body>
</html>

app file

var mainModule = angular.module('myApp', ['ui.bootstrap.dialog']);
function viewServiceConfig($routeProvider) {
$routeProvider.
    when('/', {
        controller: SomeController,
        templateUrl: 'somehtml.html'
    }).



    when('/someroute', {
        controller: SomeRouteController,
        templateUrl: 'someroutehtml.html'
    }).


    otherwise({
        redirectTo: '/'
    });
}

mainModule.config(viewServiceConfig);
3
  • Did you tried jqLite' native bind method? Like angular.element(document).bind("ready" , function(){... ? Commented Jul 30, 2013 at 11:07
  • It's not clear how you're navigating between "pages" (are these ng-views, ng-includes full document requests, ...?) but generally, you don't need to bind to DOMContentLoaded with Angular. Instead, you'd create a Directive. Commented Jul 30, 2013 at 11:11
  • tnx @AndréDion . i use ng-view and use route in module configuration like this when('/', { controller: SomeController, templateUrl: 'somehtml.html' }). Commented Jul 30, 2013 at 11:16

3 Answers 3

40

You can listen in your controllers defined in routes i.e. SomeController and SomeRouteController for $viewContentLoaded event. $viewContentLoaded is emitted every time the ngView content is reloaded and should provide similar functionality as the document.ready when routing in angularjs:

function SomeController($scope) {
   $scope.$on('$viewContentLoaded', function() {window.scrollTo(0,90);});
}

The document.ready is also triggered only once when you load your index.html. It is not triggered when the partial templates defined in your route configuration are loaded.

Sign up to request clarification or add additional context in comments.

3 Comments

tnx . i use your solution and debug it with firebug . it scrolls page to that point but after page loads completely something reset scroll to top of page
@SoheilGh Maybe try to change the listener to this function(event) {event.preventDefault(); window.scrollTo(0,90);}
tnx you . i use firebug to find out after which event page scrolls to top and i find it . i changed the event to $routeChangeSuccess and it works finally
5

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.

Comments

4

I was able to apply the scroll event through Dave's answer and by using routeChangeSuccess

function SomeController($scope) {
    $scope.$on('$routeChangeSuccess', function() {window.scrollTo(0,90);});
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.