-->

angularjs route - jump to specific page section on

2020-07-20 04:55发布

问题:

I am trying to make some kind of a mix between an Angular anchor and routing...

I do have it working in the home page, since the anchor sections are there, however, if I am in another page, it does not.

Can anyone point me in the right direction on how to do it correctly, please?

Here´s what I have so far

freddoApp.config(function($routeProvider, $locationProvider) {
    $routeProvider

    // route for the home page
    .when('/', {
        templateUrl : 'pages/home/home.html',
        controller  : 'mainController'
    })

    // route for the productos page
    .when('/productos', {
        templateUrl : 'pages/home/home.html',
        controller  : 'mainController'
    })

    // route for the unico page
    .when('/unico', {
        templateUrl : 'pages/home/home.html',
        controller  : 'mainController'
    })

    // route for the sabores page
    .when('/sabores', {
        templateUrl : 'pages/home/home.html',
        controller  : 'mainController'
    })

    // route for the locales page
    .when('/locales', {
        templateUrl : 'pages/locales/locales.html',
        controller  : 'storeController'
    })

    // route for the servicios page
    .when('/servicios', {
        templateUrl : 'pages/servicios/servicios.html',
        controller  : 'servicesController'
    })

    // route for the about page
    .when('/about', {
        templateUrl : 'pages/about/about.html',
        controller  : 'aboutController'
    })

    // route for the contact page
    .when('/contact', {
        templateUrl : 'pages/contact/contact.html',
        controller  : 'contactController'
    });

    // use the HTML5 History API
    $locationProvider.html5Mode(true);
});

/............................./

    freddoApp.controller('mainController', function($scope, $location, $anchorScroll) {

    $scope.scrollTo = function(id) {
        $location.hash(id);
        $anchorScroll();
    };

/............................./

(HTML)

<div id="freedo-nav-bar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li><a ng-click="scrollTo('productos')">Productos</a></li>
                    <li><a ng-click="scrollTo('unico')"> Freddo Único</a></li>
                    <li><a ng-click="scrollTo('sabores')"> Sabores</a></li>
                    <li><a href="#locales"> Locales</a></li>
                    <li><a href="#servicios"> Servicios</a></li>
                    <li><a href="#about"> Nosotros</a></li>
                    <li><a href="#contact"> Contacto</a></li>
                </ul>
            </div>

Thanks!

回答1:

If i understand you right, i think you could solve this with resolve.

First add a resolve function to your routing:

.when('productos/', {
    templateUrl : 'pages/home/home.html',
    controller  : 'mainController',
    resolve: {
        anchorname: function() { {
            // anchor name
            return 'productos'
        }
    }
})

In your controller pass the resolve object and add some function for the scrolling

freddoApp.controller('mainController', function($scope, $location, $anchorScroll, anchorname) {

    if(anchorname){
        $location.hash(anchorname);
        $anchorScroll();
    }

})

This should immediately scroll to the anchor after you selecting the route.

EDIT: Its working, see here: https://jsfiddle.net/326f44xu/



回答2:

Best approach for you is using routing url params like /home/:section. If you do it in that way, you are able to access from any other page. PLUNKER

ROUTE CONFIG

app.config(function($routeProvider, $locationProvider) {
  $routeProvider
  .when('/home/:section?', {
    templateUrl: 'home.html',
    controller: 'mainController'
  }) //You don't need to repeat your .when() multiple times

  $routeProvider.otherwise({
    redirectTo: '/home'
  });
});

HOME CTRL (mainController)

app.controller('mainController', function($routeParams, $location, $anchorScroll) {
  //wrap this on $onInit or activate() function if you want
  $location.hash($routeParams.section);
  $anchorScroll();
});

HOME.HTML

<div><!-- HOME --></div>
<div id="productos"><!-- Productos--></div>
<div id="unico"><!-- unico--></div>
<div id="sabores"><!-- sabores--></div>

INDEX.HTML

<body>
  <div>
    <a ng-href="#/home">Home</a>
    <a ng-href="#/home/productos">productos</a>
    <a ng-href="#/home/unico">Unicos</a>
    <a ng-href="#/home/sabores">Sabores</a>
  </div>

  <div ng-view></div>
</body>

** You can use empty route with optional params like /:section?, but I added /home to make it clear. The ? at the end of url param is to make it optional.