Active class is not working using Angular.js ui-router - angularjs

I have one issue while using Angular.js ui-router.I added active class for highlight the menu but its not working.I am explaining my code below.
<ul class="nav navbar-nav">
<li ui-sref-active="active" ><a ui-sref=".profile">College Profile</a></li>
<li ng-class="{active: $state.includes('dashboard.deptmanagement')}"><a ui-sref="dashboard.deptmanagement.stream" >Department Management</a></li>
<li ui-sref-active="active">
<li ui-sref-active="active"><a ui-sref="dashboard.user.usermanagement">User Management</a></li>
<li ui-sref-active="active"><a ui-sref="dashboard.plan.contime">Plan Management</a></li>
</ul>
My problem is in Department Management menu from the above code.As this menu has some sub menus if i am adding here ui-sref-active="active" the issue is coming when the sub menu are selecting this menu can not get any active class.The sub menu of this parent menu is given below.
deptmanagement.html:
<div>
<tabset>
<tab ui-sref=".stream" ui-sref-active="active">
<tab-heading>Add Stream</tab-heading>
</tab>
<tab ui-sref=".dept" ui-sref-active="active">
<tab-heading>Add Department</tab-heading>
</tab>
<tab ui-sref=".course" ui-sref-active="active">
<tab-heading>Add Course</tab-heading>
</tab>
<tab ui-sref=".sub" ui-sref-active="active">
<tab-heading>Add Subject</tab-heading>
</tab>
</tabset>
<div ui-view></div>
</div>
loginroute.js
var Admin=angular.module('Channabasavashwara',['ui.router', '720kb.datepicker','ngMessages','ngCapsLock','ui.bootstrap']);
Admin.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('/', {
url: '/',
templateUrl: 'dashboardview/login.html',
controller: 'loginController'
})
.state('dashboard', {
url: '/dashboard',
templateUrl: 'dashboardview/dashboard.html',
controller: 'dashboardController'
})
.state('dashboard.profile', {
url: '/profile',
templateUrl: 'dashboardview/profile.html',
controller: 'profileController'
})
.state('dashboard.deptmanagement', {
url: '/deptmanagement',
templateUrl: 'dashboardview/deptmanagement.html',
controller: 'deptmanagementController'
})
.state('dashboard.deptmanagement.stream', {
url: '/stream',
templateUrl: 'dashboardview/stream.html',
controller: 'streamController'
})
.state('dashboard.deptmanagement.course', {
url: '/course',
templateUrl: 'dashboardview/course.html',
controller: 'resourcecourseController'
})
.state('dashboard.deptmanagement.sub', {
url: '/subject',
templateUrl: 'dashboardview/subject.html',
controller: 'deptsubjectController'
})
.state('dashboard.deptmanagement.dept', {
url: '/dept',
templateUrl: 'dashboardview/dept.html',
controller: 'deptController'
})
})
Here my requirement is when any of above sub-menu will select the main menu remain highlight.Please help me to resolve this issue.

To use ng-class="{'active':$state.includes('route1')}" for navigation $state needs to be available to the view, ie defined on the $scope in the controller.
$scope.$state = $state;
You can define $rootScope.$state = $state; so that it is available on $rootScope throughout your app although this may be considered poor practice.
If your controller is mainCtrl then:
Admin.controller('mainCtrl', ['$scope', '$state', function($scope, $state) {
$scope.$state = $state;
}]);
Or if you want to define $state on $rootScope you can use .run()
Admin.run(function($rootScope, $state) {
$rootScope.$state = $state;
})
Because the submenu is a tabset you can compare the current state name for each tab to set the active tab
<div>
<tabset>
<tab ui-sref="dashboard.deptmanagement.stream" active="$state.current.name == 'dashboard.deptmanagement.stream'">
<tab-heading>Add Stream</tab-heading>
</tab>
<tab ui-sref="dashboard.deptmanagement.dept" active="$state.current.name == 'dashboard.deptmanagement.dept'">
<tab-heading>Add Department</tab-heading>
</tab>
<tab ui-sref="dashboard.deptmanagement.course" active="$state.current.name == 'dashboard.deptmanagement.course'">
<tab-heading>Add Course</tab-heading>
</tab>
<tab ui-sref="dashboard.deptmanagement.sub" active="$state.current.name == 'dashboard.deptmanagement.sub'">
<tab-heading>Add Subject</tab-heading>
</tab>
</tabset>
<div ui-view></div>
</div>

Related

ng-click not working with mmenu

I trying to use mmenu menu http://mmenu.frebsite.nl/ with my angularJS base app.
ui-sref is working but ng-click is not working with mmenu link.
Can anyone help me, what wrong I am doing it.
Main HTML Page
<body ng-app="student">
</div>
<div class="header">menu</div>
<div class="content">
<div ui-view></div>
</div>
<nav id="menu">
<ul>
<li ><a ng-href="" ng-click="holidayInfo()">Holiday</a></li>
<li><a ui-sref="notices">Notices</a></li>
</ul>
</nav>
</div>
</body>
app.js
app.config(['$urlRouterProvider', '$stateProvider', '$locationProvider','RestangularProvider',
function ($urlRouterProvider, $stateProvider, $locationProvider, RestangularProvider ) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state("home", {
url: "/",
templateUrl: 'home.html',
controller:'homeCtrl as vm',
resolve: {
currentyear: function (Restangular, $stateParams) {
return Restangular.one('CurrentACYear').get();
}
}
})
.state("notices", {
url: "/notices",
templateUrl: 'notices.html',
controller:'schoolNoticesCtrl as vm',
resolve: {
notciesList: function (Restangular, $stateParams) {
return Restangular.one('notices').get();
}
}
})
My holidayInfo() method is in homeCtrl controller.
When I do <nav id="menu" ng-controller="homeCtrl"> it throw error, not able to resolve currentyear .
You had # in the href of Holiday which is executing $urlRouter.otherwise code which is fallback URL.
It should be kept as blank
ng-href=""
Other thing would be you havent't had nav bar wrap with scope of controller where you wrote holidayInfo method. There you can have new controller for your navbar component & have holidayInfo method over there.
Markup
<nav id="menu" ng-controller="navbarCtrl">
<ul>
<li><a ng-href="#" ng-click="holidayInfo()">Holiday</a></li>
<li><a ui-sref="notices">Notices</a></li>
</ul>
</nav>
Code
app.controller('navbarCtrl', function(){
$scope.holidayInfo = function () {
console.log("i am trigeered ....")
};
})
Do update question with more info if you still found an issue.

Checkout Step Navigation

i have no idea how to solve this Problem.
I have 4 Checkout Steps. Each Step has an Form to fullfill.
If Form is valid, the next Step in Navigation should be activated.
This is the Routing Script
"use strict";
var router = angular.module("router", ["ui.router"]);
router.config(["$stateProvider", "$urlRouterProvider", "$locationProvider", "$httpProvider",
function($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {
$locationProvider.html5Mode({
enabled: true,
requireBase: true,
rewriteLinks: true
});
$stateProvider
.state("step1", {
url: "/step1",
controller: "Step1Controller",
templateUrl: "app/views/step1-partial.html"
})
.state("step2", {
url: "/step2",
controller: "Step2Controller",
templateUrl: "app/views/step2-partial.html"
})
.state("step3", {
url: "/step3",
controller: "Step3Controller",
templateUrl: "app/views/step3-partial.html"
})
.state("step4", {
url: "/step4",
controller: "Step4Controller",
templateUrl: "app/views/step4-partial.html"
});
$urlRouterProvider.otherwise("/step1");
}
]);
router.run(function($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
});
and this is one of the Partials View
<section class="steps">
<ul class="clear-list">
<li class="active">Step1</li>
<li>Step2</i></li>
<li>Step3</li>
<li>Step4</li>
</ul>
</section>
<form name="registerForm" ng-cloak>
formfields...
</form>
<div class="step-footer">
Next >
</div>
can somebody help me out
In processData() function (which i am hoping you will we having in all three controllers) after successful processing of data you can do $location.path('/step2');
and so on for all the steps.
For activating the current section in which currently you are, you can have some common variable in all three controller and set the value of that variable like ($scope.currentStep = step1) respectively.
Example.
<section class="steps">
<ul class="clear-list">
<li ng-class="{active : currentStep=='step1'}">Step1</li>
<li ng-class="{active : currentStep=='step2'}">Step2</i></li>
<li ng-class="{active : currentStep=='step3'}">Step3</li>
<li ng-class="{active : currentStep=='step4'}">Step4</li>
</ul>
</section>
use ui-sref-active="active" instead of class

Getting multiple select when reloading the page using angular.js

I have one issue.I am using Angular.js ui-router for displaying the nested views.In my case i have one parent menu and it has some sub menus. Let me to explain my code below.
dashboard.html:
<li ui-sref-active="active" ><a ui-sref="dashboard">Home</a></li>
<li ui-sref-active="active">
<a ui-sref=".res.userrole">Resource Management</a>
</li>
Here Resource Management is my parent menu and its sub menus are given below.
res.html:
<tabset>
<tab ui-sref="dashboard.res.userrole" ui-sref-active="active">
<tab-heading>Add User Role</tab-heading>
</tab>
<!--<tab ui-sref="dashboard.res.course" ui-sref-active="active">
<tab-heading>Add Course</tab-heading>
</tab>-->
<tab ui-sref="dashboard.res.class" ui-sref-active="active">
<tab-heading>Add Class</tab-heading>
</tab>
<tab ui-sref="dashboard.res.section" ui-sref-active="active">
<tab-heading>Add Section</tab-heading>
</tab>
<tab ui-sref="dashboard.res.session" ui-sref-active="active">
<tab-heading>Add Session</tab-heading>
</tab>
<tab ui-sref="dashboard.res.unit" ui-sref-active="active">
<tab-heading>Add Unit</tab-heading>
</tab>
</tabset>
<div ui-view></div>
Loginroute.js:
.state('dashboard.res', {
url: '/res',
templateUrl: 'dashboardview/res.html',
controller: 'resController'
})
.state('dashboard.res.userrole', {
url: '/userrole',
templateUrl: 'dashboardview/userrole.html',
controller: 'resourceuserroleController'
})
.state('dashboard.res.class', {
url: '/class',
templateUrl: 'dashboardview/class.html',
controller: 'resourceclassController'
})
.state('dashboard.res.section', {
url: '/section',
templateUrl: 'dashboardview/section.html',
controller: 'resourcesectionController'
})
.state('dashboard.res.session', {
url: '/session',
templateUrl: 'dashboardview/session.html',
controller: 'resourceSessionController'
})
.state('dashboard.res.unit', {
url: '/unit',
templateUrl: 'dashboardview/unit.html',
controller: 'resourceunitController'
})
Here my problem is when i am reloading any page of the above sub menu the first sub menu('i.e-Add User Role') is getting active class along with the refreshing sub menu page.So at a time i am getting two sub menu are active.Here i need if user is reloading any sub menu page that sub menu title will remain active.Please help me to resolve this issue.
Here is a idea using $state.includes().
e.g:
if state is /home/:id, Home tab will be activated.
if state is /home/sub/:id, Home and Home sub tabs will be activated
code example:
<ul class="nav navbar-nav">
<li ng-class="{active: $state.includes('home')}" class="active"><a ui-sref="home" href="/">Home</a></li>
<li ng-class="{active: $state.includes('home.sub')}" ><a ui-sref="home.sub" href="/">Home sub</a></li>
<li ng-class="{active: $state.includes('reports')}" ><a ui-sref="reports" href="/admin/reports">Reports</a></li>
<li ng-class="{active: $state.includes('users')}" ><a ui-sref="users" href="/admin/users">Users</a></li>
<li ng-class="{active: $state.includes('groups')}" ><a ui-sref="groups" href="/admin/groups">Groups</a></li>
Hope this may help to your case.

AngularJS multilanguage routing

I have an AngularJS webapp. I´m changing the application so the URLs can be multilanguage (for SEO indexing purposes).
I´m setting up my app.js, like this:
$routeProvider.when('/:language', {templateUrl: 'partials/home.html', controller: 'HomeCtrl'});
$routeProvider.when('/:language/about', {templateUrl: 'partials/about.html', controller: 'AboutCtrl'});
Then, in a service I get the language parameter with $routeParams and call my translation code with angular-translate to serve the page in the corresponding language.
Everything is working so far.
But, moreover, in the menu bar, I have a select combo box to choose the language. So, when user change the language, the url language parameter should change.
How should I do this?
Here's an example. Like I said in your other question, I'd use ui-router for this.
http://plnkr.co/edit/bCNgS07BblMHz55VBRSQ?p=preview
The language dropdown will preserve the currently selected state. So if you go to home -> paragraph, then change language, you will remain on the paragraph route but the language parameter will change.
app.js:
routerApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/en');
$stateProvider
.state('lang', {
url: '/:language',
templateUrl: 'partial-lang.html',
abstract: true,
controller: function($state, $scope) {
$scope.changeLanguage = function(language) {
$state.go($state.current.name, {language: language});
}
}
})
.state('lang.home', {
url: '',
templateUrl: 'partial-home.html'
})
.state('lang.home.list', {
url: '/list',
templateUrl: 'partial-home-list.html',
controller: function($scope) {
$scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
}
})
.state('lang.home.paragraph', {
url: '/paragraph',
template: 'I could sure use a drink right now.'
})
.state('lang.about', {
url: '/about',
templateUrl: 'partial-about.html'
});
});
partial-lang.html:
<nav class="navbar navbar-inverse" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" href="#">AngularUI Router</a>
</div>
<ul class="nav navbar-nav">
<li><a ui-sref=".home">Home</a></li>
<li><a ui-sref=".about">About</a></li>
<li class="dropdown">
<a href class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Language: {{$state.params.language | uppercase}} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href ng-click="changeLanguage('en')">English</a></li>
<li><a href ng-click="changeLanguage('fr')">Français</a></li>
<li><a href ng-click="changeLanguage('ru')">Русский</a></li>
</ul>
</li>
</ul>
</nav>
<div class="container">
<div ui-view></div>
</div>
index.html
<body ng-app="routerApp">
<div ui-view></div>
<pre>
<p>state = {{$state | json}}</p>
</pre>
</body>
The rest of the files are self-explanatory
You bind your combo box to a variable, then you can use $watch to know when it changes, and finally you can user $route to update language.
$watch info
$route info
Update:
Something like this:
$scope.$watch('language', function (newValue, oldValue) {
if (newValue !== oldValue) {
$route.updateParams({language: newValue});
}
});
UPDATE VERSION 1.0.7
$scope.$watch('language', function (newValue, oldValue) {
if (newValue !== oldValue) {
var path = $location.path();
path = path.replace('/'+oldValue+'/', '/'+newValue+'/');
console.log(path);
$location.path(path);
$route.reload();
}
});

How can I create a link in an angular app that loads inside a tab?

I have a link in an angular app that looks like this:
<tab ng-controller="childCtrl" heading="Children">
edit
</tab>
How can I load the link inside the tab instead of refreshing the entire view?
I like other approach with one point of handling all routes. ui.router with nested views allows to do it.
Template
<div ><a ui-sref="home.tab1" >Tab1</a></div>
<div><a ui-sref="home.tab2" >Tab2</a></div>
<br>
<div ui-view="tabs"></div>
app
var app = angular.module('plunker', ['ui.router']);
app.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'main.html',
controller: 'MainCtrl',
virtual: true
})
.state('home.tab1', {
url: '/tab1',
views: {
tabs: {
templateUrl: '1.html'
}
}
})
.state('home.tab2', {
url: '/tab2',
views: {
tabs: {
templateUrl: '2.html'
}
}
});
$urlRouterProvider
.otherwise('/home/tab1');
})
And demo http://plnkr.co/edit/r1jPgkMnPAMlI34gZrMA?refresh&p=preview
You could define the path to the selected tab's content when the link is clicked, and use ng-include to display it.
<div ng-repeat="parent in parents">
<div ng-repeat="child in parent.children">
<a href="" ng-click="selectPath(parent, child);">
Parent {{parent.Id}}, Child {{child.Id}}
</a>
</div>
</div>
<div ng-include="selected.path"></div>
Here is a demo: http://plnkr.co/edit/7SPnluxUfcNNQDyeC6yt?p=preview

Resources