index.html
<body ng-app="dnaClientApp" ng-controller="MainCtrl">
<ul class="nav navbar-nav navbar-collapse">
<li ng-repeat="item in menu" ng-class="{active: item.route === menuActive }"><a href="#{{item.route}}">{{item.label}}</a>
</li>
</ul>
MainCtrl main.js
.controller('MainCtrl', function ($scope, $route, $rootScope, $location, $cookies, translationSvc, $http, $cookieStore, UserService) {
if (typeof $scope.selectedLanguage === 'undefined') {
$scope.selectedLanguage = 'fi';//Default language
}
translationSvc.getTranslation($scope, $scope.selectedLanguage);
$scope.menu = translationSvc.getMenu($scope.selectedLanguage);//Creating menus dynamically
console.log('$scope.menu-->' + $scope.menu + ' length: ' + $scope.menu.length);
$scope.menuActive = '/';
$rootScope.$on('$routeChangeSuccess', function (e, curr, prev) {
$scope.menuActive = $location.path();
});
translationSvc
var getMenu = function($scope, language) {
if (language === 'fi' && typeof $rootScope.userRole !== 'undefined' && typeof $rootScope.userRole.currentUserRole !== 'undefined' && $rootScope.userRole.currentUserRole.userRole === 3 ) {
$scope.menu = [
{label: 'Home', route: '#/'},
{label: 'DNA', route: '#/dna'},
{label: 'Dna-list', route: '#/dna-list'},
{label: 'Admin', route: '#/admin'}
]
}else...
Routes after login goes here, menu is in index.html
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
Question
If I check the length of menu array in controller, everything is ok, but my menus in header are all the same, default menus (3 menus, not 4). What am I doing wrong or why I can't update menus dynamically? Idea is to get different menus for different roles, like administrator could see a admin menu as well etc. Any other working ideas to get this done?
ifstatement met?