Angularjs route - go to a specific section of the page by route link

I am trying to make some kind of combination between anchor and Angular routing ...

I have it working on the home page as there are anchor sections, however if I am on another page it is not.

Can anyone point me in the right direction on how to do this correctly?

That'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>

      

Thank!

+3


source to share


2 answers


If I understand you correctly, I think you could work it out with permission.

First add the resolution function to your routing:

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

      

In controller pass permission object and add some function for scrolling



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

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

})

      

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

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

+6


source


The best approach for you is to use routing url parameters like /home/:section

. If you do this, you will be 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 an empty route with optional parameters like /:section?

but I added /home

to make it clear. ?

at the end of the url param is to make it optional.

+4


source







All Articles