0

What approach do you recommend to navigate pages from nav menu using angularjs? Pages will have footer and header html using ng-include to have common templates. I found ng-view to navigate between pages.

2 Answers 2

1

In this example, there's a module named navList and a controller and a method navClass inside it. Using a special service in angular.js, $location which parses the URL in the browser and makes the URL available in the application. Finally, just match the URL hash with the parameter passed.

var navList = angular.module('navList', []);

navList.controller('navCtrl', ['$scope', '$location', function ($scope, $location) {
    $scope.navClass = function (page) {
        var currentRoute = $location.path().substring(1) || 'home';
        return page === currentRoute ? 'active' : '';
    };        
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="well sidebar-nav" ng-app="navList">
    <ul class="nav nav-list" ng-controller="navCtrl">
        <li ng-class="navClass('home')"><a href='#/home'>Home</a></li>
        <li ng-class="navClass('about')"><a href='#/about'>About Us</a></li>
        <li ng-class="navClass('contact')"><a href='#/contact'>Contact Us</a></li>
    </ul>
</div>

JsFiddle Link

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

Comments

1

Angular router is what you need:

https://github.com/angular-ui/ui-router/blob/master/README.md

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.