Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular injected pages with long content make entire page load below the top of the page, cutting off the page title

See how in the plunker below, when you click on link one or link three, the pages do not load at the top of the page so you cannot see the page title, you have to actually scroll up to see the page title. Why is this? How can I make it so it loads the page at the top, like a regular webpage?

Here's the plunker.

Here's my js:

var routerApp = angular.module('routerApp', ['ui.router','ui.bootstrap']);

routerApp.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $urlRouterProvider.otherwise('/home');
    $locationProvider.html5Mode(false).hashPrefix("");
    $stateProvider


        // HOME VIEW ========================================
        .state('home', {
            url: '/home',
            templateUrl: 'partial-home.html'
         //   onEnter: scrollContent
        })

        // ONE VIEW ========================================
        .state('one', {
            url: '/one',
            templateUrl: 'partial-one.html' 
        })

        // TWO VIEW ========================================
        .state('two', {
            url: '/two',
            templateUrl: 'partial-two.html'
        })

          // THREE VIEW ========================================
        .state('three', {
            url: '/three',
            templateUrl: 'partial-three.html'
        })
});

Here's my html (see the plunker link above for the injected pages).

<!DOCTYPE html>
<html>
  <head>
    <!-- CSS  -->
    <script data-require="jquery@*" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
    <link rel="stylesheet" href="style.css" />
    <!-- JS  -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="http://code.angularjs.org/1.2.13/angular.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.0/ui-bootstrap-tpls.js"></script>
    <script src="script.js"></script>
    <!-- // base path for angular routing   -->
    <!--<base href="/" />--> 
  </head>


<body ng-app="routerApp">


<!-- NAVIGATION -->

<!-- Fixed navbar -->
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" ui-sref="home">Some web page somewhere</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li><a ui-sref="one">Link One</a></li>
            <li><a ui-sref="two">Link Two</a></li> 
            <li><a ui-sref="three">Link Three</a></li>
          </ul>

        </div><!--/.nav-collapse -->
      </div>
    </nav>
<!-- End Fixed navbar -->




<!-- MAIN CONTENT -->
<!--  Inject Content Here -->
<div class="container" style="padding-top:68px;">
    <div ui-view></div>
</div>





</body>
</html>
like image 736
Agent Zebra Avatar asked Jul 15 '15 17:07

Agent Zebra


1 Answers

Add autoscroll="false" to your ui-view to prevent this behavior.

<div class="container" style="padding-top:68px;">
    <div ui-view autoscroll="false"></div>
</div>

Plunker demo

See the documentation about ui-view and autoscroll directives:

autoscroll (optional)

string

It allows you to set the scroll behavior of the browser window when a view is populated. By default, $anchorScroll is overridden by ui-router's custom scroll service, ui.router.state.$uiViewScroll. This custom service let's you scroll ui-view elements into view when they are populated during a state activation.

like image 151
aw04 Avatar answered Nov 17 '22 00:11

aw04