user3786596 user3786596 - 10 months ago 671
AngularJS Question

Angular Material Nav Bar Set Default Active Item

I am experimenting with an angular app using Angular Material and cannot seem to get my navbar to set the default item to be selected. I am using the Angular Material Navbar (Directive Info) for displaying my views and here is my template html for the navbar:

<!-- Navbar -->
<md-content class="md-padding">
<md-nav-bar md-selected-nav-item="currentNavItem" nav-bar-aria-label="navigation links">
<md-nav-item md-nav-sref="layout.home" name="home">Home</md-nav-item>
<md-nav-item md-nav-sref="" name="gallery">Gallery</md-nav-item>
<md-nav-item md-nav-sref="#" name="page3">Page Three</md-nav-item>

In my controller, I am setting up the currentNavItem to 'home' which should correspond to my md-nav-item named home which I felt should set it to be selected based on

export default class HeaderController {
constructor($log) {
Object.assign(this, {
this.currentNavItem = 'home';

$onInit() {

Any help would be appreciated as to why this doesn't make my list item selected by default when I load the app.


The issue in my case was that my controller was not being used in the navbar selection `md-selected-nav-item="$ctrl.currentNavItem"'. Adding values for each md-nav-item didn't seem to affect the active item setting.


You need to set the value attribute of each md-nav-item - CodePen


<div ng-controller="AppCtrl as vm" ng-cloak="" ng-app="MyApp">
  <!-- Navbar -->
  <md-content class="md-padding">
    <md-nav-bar md-selected-nav-item="vm.currentNavItem" nav-bar-aria-label="navigation links">
      <md-nav-item md-nav-sref="layout.home" name="home" value="home">Home</md-nav-item>
      <md-nav-item md-nav-sref="" name="gallery" value="gallery">Gallery</md-nav-item>
      <md-nav-item md-nav-sref="#" name="page3" value ="page3">Page Three</md-nav-item>


angular.module('MyApp',['ngMaterial', 'ngMessages'])

.controller('AppCtrl', function() {
  this.currentNavItem = "home";