toggling class name based on route changes using angular-route - angularjs

have 3 routes containing 3 forms Im trying to set bootstrap active class on current tab based on the current route in angular.I used angular route module. How can I achieve this. I m attaching the js code please check and help please
plnkr.co/edit/iTgNTJ74iLzlNx902qfP?p=preview

I used this.tab = 1 to default the tab where the class will be "active" first. Here's are my controller additions:
angular.module('ciwiseGenledgerApp')
.controller('MainCtrl', function () {
this.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
this.tab = 1;
this.selectTab = function(setTab) {
this.tab = setTab;
};
this.isSelected = function(checkTab){
return this.tab === checkTab;
};
});
I used ng-controller="MainCtrl as ctrl" in my page. Here's the snippet on the page.
<div class="collapse navbar-collapse" id="js-navbar-collapse" ng-controller="MainCtrl as ctrl">
<ul class="nav navbar-nav">
<li ng-class="{ active: ctrl.isSelected(1) }">Home</li>
<li ng-class="{ active: ctrl.isSelected(2) }"><a ng-href="#/reports" ng-click="ctrl.selectTab(2)">Reports</a></li>
<li ng-class="{ active: ctrl.isSelected(3) }"><a ng-href="#/admin" ng-click="ctrl.selectTab(3)">Admin</a></li>
<li ng-class="{ active: ctrl.isSelected(4) }"><a ng-href="#/help" ng-click="ctrl.selectTab(4)">Help</a></li>
<li ng-class="{ active: ctrl.isSelected(5) }"><a ng-href="#/about" ng-click="ctrl.selectTab(5)">About</a></li>
<li ng-class="{ active: ctrl.isSelected(6) }"><a ng-href="#/contact" ng-click="ctrl.selectTab(6)">Contact</a></li>
</ul>
</div>
My active tab code is independent of the view routing. Here's my app.js. It has the routing code for my views.
angular
.module('ciwiseGenledgerApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch'
])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl',
controllerAs: 'about'
})
.when('/contact', {
templateUrl: 'views/contact.html',
controller: 'ContactCtrl',
controllerAs: 'contact'
})
.otherwise({
redirectTo: '/'
});
});
$.material.init();

Related

ngView with Node.js doesn't load the main page when the ULR is reloaded

In the first load everything is okay but, when I reload/refresh the page the main page don't load and only the template load.
I use Node.js and express for route pages I think when I try reload page:
app.get('/', function (req, res) {
res.render('index.html');
console.log('GET: 200 index');
console.log(req.statusCode);
<div class="container conteudo btn-group btn-group-lg" ng-controller="routeCtl">
<a class="btn btn-default" ng-class="{active: isActive('/cadastro/cadastro')}" href="/cadastro/cadastro">{{lnkCadastrar}}</a>
<a class="btn btn-default" ng-class="{active: isActive('/login/login')}" href="/login/login">{{lnkAcessar}}</a>
<a class="btn btn-default" ng-class="{active: isActive('/home/home')}" href="/home/home">{{lnkHome}}</a>
</div>
<div ng-view class="container divNgView"></div>
This is my route:
var appRoute = angular.module('appRoute', ['ngRoute'])
.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$routeProvider.when('/footer', {
templateUrl: '../views/footer.ejs'
}).when('/login/login', {
templateUrl: '../views/login/login.html',
controller: 'loginCtl'
}).when('/cadastro/cadastro', {
templateUrl: '../views/cadastro/cadastro.html',
controller: 'cadastroControll',
controllerAs: 'cadastroCtl',
reloadOnSearch: false
}).when('/home/home', {
templateUrl: '../views/home/home.html',
controller: 'homeControll'
}).when('/*', {
templateUrl: '../views/error.ejs'
});
$routeProvider.otherwise({
}).otherwise({
redirectTo: '/index.html',
reloadOnSearch: false
});
$locationProvider.html5Mode({enabled: true, requireBase: false});
}]);
appRoute.controller('appRouteCtl', ['$scope', function ($scope) {
$scope.title = "Controlador de Patio";
}]);

nested ui-router $location binding

I have some problem with binding when i use ui-router. I am trying to make the app modular and keep it clean and simple.
I have the following app.js
// main routing - index.html
var app = angular.module('mainApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('index', {
url: '/',
templateUrl: 'pages/main.html'
})
.state('cars', {
url: '/cars',
templateUrl: 'pages/Cars_model/main.html'
})
.state('cars.audi', {
url: '/audi',
templateUrl: 'pages/Cars_model/audi.html'
})
.state('cars.ford', {
url: '/ford',
templateUrl: 'pages/Cars_model/ford.html'
})
});
app.controller('indexController', function($scope, $location) {
$scope.isIndexPage = function() {
return $location.path() === '/';
}
});
app.controller('carsCtrl', function($scope, $location) {
if($location.path() === '/cars/audi')
{
$scope.pageHeader = "AUDI";
$scope.curentMenu = "Best Cars";
$scope.title = "Audi Specs";
}
if($location.path() === '/cars/ford')
{
$scope.pageHeader = "FORD";
$scope.curentMenu = "Best Cars";
$scope.title = "Ford Specs";
}
});
and the file where i want to use binding
<div class="container" ng-controller="carsCtrl">
<div class="page-header">
<h1>{{pageHeader}}</h1>
</div>
<!-- The first row -->
<div class="row">
<div class="col-lg-12 col-md-12 col-sd-12">
<ol class="breadcrumb">
<li>Home</li>
<li>{{curentMenu}}</li>
<li class="active">{{pageHeader}}</li>
</ol>
</div>
</div>
</div>
The problem is when i load the page the binding work only once, for the second i have to refresh the page.
I am not sure if i used the corect logic
if($location.path() === '/cars/ford')
I think i found the solution
Using
ui-sref-opts="{reload: true}"
in the menu solve the problem
<a ui-sref="cars.audi" ui-sref-opts="{reload: true}">Audi</a>
Plunker

setting active menu in angular js with routing

Newbie in angularjs. Trying to learn some new stuffs.
I have a simple page and when the user navigates to different page I need to set active class on respective li of the menuitem. Below is the way I have added routes.
angular.module('home', ["ngRoute"]).
config(function ($routeProvider, $locationProvider) {
$routeProvider.
when('/home', { templateUrl: 'partials/home.html', controller: "homeController", activetab: "home" }).
when('/about', { templateUrl: 'partials/about.html', activetab: "about" }).
when('/gallery', { templateUrl: 'partials/gallery.html', activetab: "gallery" }).
when('/contact', { templateUrl: 'partials/contact.html', activetab: "contact" }).
when('/services', { templateUrl: 'partials/services.html', activetab: "services" }).
when('/house', { templateUrl: 'partials/house.html', activetab: "house" }).
otherwise({ redirectTo: '/home', templateUrl: 'partials/home.html', activetab: "home" });
$locationProvider.html5Mode(true);
}).controller("homeController", function ($scope, $http, $route) {
$scope.$route = $route;
});
And I will have only homecontroller which is binded only when user visits home page. Other pages will have only static data.
I've added ng-app to my html as below:
<html ng-app="home">
Below is how I've tried to add active class to li.
<ul class="nav navbar-nav">
<li ng-class="{'active': $route.current.activetab == 'home'}">Home <span class="sr-only">(current)</span></li>
<li ng-class="{'active': $route.current.activetab == 'about'}">About</li>
<li ng-class="{'active': $route.current.activetab == 'services'}">Services</li>
<li ng-class="{'active': $route.current.activetab == 'house'}">The house</li>
<li ng-class="{'active': $route.current.activetab == 'gallery'}">Gallery</li>
<li ng-class="{'active': $route.current.activetab == 'contact'}">Contact</li>
</ul>
But I had no luck in completing this. Even though navigation happens the active class does not gets added. I've followed answer mentioned in this post. Please let me know how else I can achieve this or what is my mistake here?
try this jsfiddle example I made, the reason it does not work is after you switch state, you do not have access to the $route. the $route is only accessible in your homecontroller's $scope, which I assume only applying to your home page.

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

Undefined Controller AngularJS

My template is loading but I get an error that the controller is undefined. The controller does exist in my sources exactly at the location defined. What is wrong with this code?
Error: [ng:areq] Argument '/Scripts/controllers/wizardNavigationCtrl.js' is not a function, got undefined
http://errors.angularjs.org/1.2.14/ng/areq?p0=%2FScripts%2Fcontrollers%2FwizardNavigationCtrl.js&p1=not%20aNaNunction%2C%20got%20undefined
Index.html (root page)
<div class="container">
<div ui-view></div>
</div>
wizardLayout.html
<div>
<ul class="nav nav-tabs">
<li ng-repeat="model in models" ng-class="active: model.active">
<a ui-sref=".model-{{model.name}}">model.name</a>
</li>
<li ng-class="navStep=='addnew' ? 'active' : ''">
<a ui-sref=".newmodel"><span class="glyphicon glyphicon-plus"></span></a>
</li>
<li><a ui-sref=".other">Other</a></li>
<li><a ui-sref=".customer">Customer Info</a></li>
<li><a ui-sref=".shipping">Shipping Info</a></li>
<li><a ui-sref=".review">Review</a></li>
</ul>
</div>
<div ui-view>
</div>
app.js:
'use strict';
var myApp = angular.module('myApp', ['ui.router']);
myApp.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('wizard', {
url: '/',
templateUrl: 'Scripts/templates/wizardLayout.html',
controller: 'Scripts/controllers/wizardNavigationCtrl.js'
})
.state('wizard.newmodel', {
url: '/newmodel',
templateUrl: 'Scripts/templates/wizardModel.html',
controller: 'Scripts/controllers/wizardModelCtrl.js'
})
.state('wizard.other', {
url: '/other',
templateUrl: 'Scripts/templates/wizardOther.html',
controller: 'Scripts/controllers/wizardOtherCtrl.js'
})
.state('wizard.customer', {
url: '/customer',
templateUrl: 'Scripts/templates/wizardCustomer.html',
controller: 'Scripts/controllers/wizardCustomerCtrl.js'
})
.state('wizard.shipping', {
url: '/shipping',
templateUrl: 'Scripts/templates/wizardShipping.html',
controller: 'Scripts/controllers/wizardShippingCtrl.js'
})
.state('wizard.review', {
url: '/review',
templateUrl: 'Scripts/templates/wizardReview.html',
controller: 'Scripts/controllers/wizardReviewCtrl.js'
});
}]);
wizardNavigationCtrl.js
myApp.controller('wizardNavigationCtrl', ['wizardSvc', '$scope', function (wizardSvc, $scope) {
alert('navigation controller! ' + wizardSvc.quote.title);
$scope.navStep = 'addnew';
wizardSvc.init();
}])
The controller should be in the format MyControllerName not the name and path to the javascript file. That in mind, in order to use the controller the javascript must have been loaded by the browser already. This requires the use of a traditional script tag because angular won't find or load these scripts for you.
Taking a snippet from your code it becomes:
.state('wizard', {
url: '/',
templateUrl: 'Scripts/templates/wizardLayout.html',
controller: 'wizardNavigationCtrl'
})
And somewhere in the page you need:
<script src="Scripts/controllers/wizardNavigationCtrl.js" type="text/javascript"></script>
Hope that helps!

Resources