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="layout.gallery" name="gallery">Gallery</md-nav-item>
<md-nav-item md-nav-sref="#" name="page3">Page Three</md-nav-item>
</md-nav-bar>
</md-content>
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 md-selected-nav-item="currentNavItem"
export default class HeaderController {
constructor($log) {
Object.assign(this, {
$log,
});
this.currentNavItem = 'home';
}
$onInit() {
this.$log.debug('HeaderController.$onInit');
this.$log.debug(this.currentNavItem);
}
}
Any help would be appreciated as to why this doesn't make my list item selected by default when I load the app.
Edit
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
Markup
<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="layout.gallery" name="gallery" value="gallery">Gallery</md-nav-item>
<md-nav-item md-nav-sref="#" name="page3" value ="page3">Page Three</md-nav-item>
</md-nav-bar>
</md-content>
</div>
js
angular
.module
(
'MyApp',
['ngMaterial', 'ngMessages']
)
.controller
(
'AppCtrl',
function()
{
this.currentNavItem = "home";
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With