Angular.js and routeprovider tutorial stackskills - angularjs

I'm currently working through an angular.js tutorial on stackskills but have run into a problem.
It seems as though my use of the routeprovider isn't correct.
Each route is supposed to correlate to a link on a nav bar. However, instead of switching views it always hits the otherwise statement and displays main as opposed to any other view.
When the otherwise statement is removed, none of the views display.
var app = angular.module("computer", ['ngRoute'])
.config(['$routeProvider', function($routeProvider){
$routeProvider.
when('/main', {
templateUrl: 'main.html',
controller:'MainCtrl'
}).
when('/about', {
templateUrl: 'about.html',
controller:'MainCtrl'
}).
when('/services', {
templateUrl: 'services.html',
controller:'ServicesCtrl'
}).
when('/contact', {
templateUrl: 'contact.html',
controller:'ContactCtrl'
}).
otherwise({redirectTo: "/main"})
}])
.controller('MainCtrl', ['$scope', function($scope){
}])
.controller('ServicesCtrl', ['$scope', function($scope){
}])
.controller('ContactCtrl', ['$scope', function($scope){
}]);
This is the div that houses the nav bar and links for the views
<div class="container">
<header class="masthead">
<h3 class="text-muted">Computer Solutions</h3>
<nav class="navbar navbar-expand-md navbar-light bg-light rounded mb-3">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav text-md-center nav-justified w-100">
<li class="nav-item active">
<a class="nav-link" href="#/main">Home <span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#/about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#/services">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#/contact">Contact</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</div>
</nav>
</header>
</div>
<div ng-controller='MainCtrl'>
<div ng-view></div>
</div>
Any advice would be greatly appreciated!

Turns out my syntax was off "#!/about" is the current method of calling a view

Related

Why is my AngularJS directive caching previous values?

I have implemented "navbar" in my web application using AngularJS directive. When the user is logged in, it displays the username of the logged in user. But, when I log in as a different user, it still displays the username of the previous user until I manually refresh the window.
Is there any solution to this?
This is the navigation-directive.html
<header class="container" ng-controller="HomeController as vm" ng-init="vm.init()">
<!-- Fixed navbar -->
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">ABC app</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul ng-if="vm.isLoggedIn()" class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#!/homeuser">Home</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#!/myappointments">My Appointments</span></a>
</li>
<li class="nav-item active">
<a class="nav-link">Welcome!<strong> {{vm.loggedInUser}} </strong><i class="fa fa-user-circle-o"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" ng-click="vm.logout()">Logout</span></a>
</li>
</ul>
</div>
</nav>
</header>
Below is the navigation-directive.js
angular.module('bbms').directive('mhNavigation', mhNavigation);
function mhNavigation(){
return{
restrict: 'E',
templateUrl: 'angular-app/navigation-directive/navigation-directive.html'
};
}
And in my HomeController I am doing
vm.loggedInUser = response.data.username;
I have copied the relevant part from my home controller

AngularJS ui-sref not working in navbar dropdown Bootstrap on mobile

Using UI-Router for Angular and Bootstrap for a navbar I'm trying to turn anchor inside dropdown clickable on but isn't working on mobile. I saw ui-sref not working in bootstrap dropdown and the solution is not working for me. I'm not getting any error on console.
HTML:
<div class="container">
<a class="mainnav-toggle is-open" data-toggle="collapse" data-target=".mainnav-collapse">
<span class="sr-only">Toggle navigation</span>
<i class="fa fa-bars"></i>
</a>
<nav class="mainnav-collapse collapse" role="navigation" aria-expanded="false" style="height: 0px;">
<ul class="mainnav-menu">
<li class="dropdown">
<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown">
Links<i class="mainnav-caret"></i>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a ui-sref="auth.add()">
<i class="fa fa-users dropdown-icon" aria-hidden="true"></i>
Link 1
</a>
</li>
<li>
<a ui-sref="auth.list()">
<i class="fa fa-history dropdown-icon" aria-hidden="true"></i>
Link 2
</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>
Reference Order:
<script src="bower_components/jquery.slimscroll/jquery.slimscroll.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js"></script>
<script src="bower_components/bootstrap-datepicker/dist/locales/bootstrap-datepicker.pt-BR.min.js"></script>
<script src="bower_components/select2/dist/js/select2.min.js"></script>
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-animate/angular-animate.min.js"></script>
<script src="bower_components/angular-loading-bar/src/loading-bar.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="bower_components/angular-local-storage/dist/angular-local-storage.min.js"></script>
<script src="bower_components/angular-messages/angular-messages.min.js"></script>
<script src="bower_components/angular-bootstrap/ui-bootstrap.min.js"></script>
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>
<script src="bower_components/angular-bootstrap-toggle/dist/angular-bootstrap-toggle.min.js"></script>
<script src="dist/app.min.js"></script>
JS: the UI-Router config:
var app = angular.module('accountsMod');
app.config(function routeConfig ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/login");
$stateProvider
.state('auth.add', {
url: '/accounts/add',
controller: 'AccountController',
templateUrl: 'app/components/account/add.html'
})
.state('auth.list', {
url: '/accounts/add',
controller: 'AccountController',
templateUrl: 'app/components/account/list.html'
})
}
The problem focus is: when I touch on these anchors in navbar collapsed according to responsive design provided by bootstrap on mobile phone, then menu closes and nothing happening.
One solution: I had to install 'angular-ui-bootstrap' package and did some changes as follow:
Referencing to directive:
var app = angular.module('accountsMod',['ui.router', 'ui.bootstrap']);
Replacing each html tag for toggle menu as follow (about old in comment):
HTML:
`
Toggle navigation
<nav class="mainnav-collapse collapse" role="navigation" aria-expanded="false" style="height: 0px;">
<ul class="mainnav-menu">
<li uib-dropdown> <!--OLD: <li class="dropdown">-->
<a uib-dropdown-toggle> <!--OLD:<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown">-->
Links<i class="mainnav-caret"></i>
</a>
<ul uib-dropdown-menu><!--OLD: <ul class="dropdown-menu" role="menu">-->
<li>
<a ui-sref="auth.add()">
<i class="fa fa-users dropdown-icon" aria-hidden="true"></i>
Link 1
</a>
</li>
<li>
<a ui-sref="auth.list()">
<i class="fa fa-history dropdown-icon" aria-hidden="true"></i>
Link 2
</a>
</li>
</ul>
</li>
</ul>
</nav>
`
So the UI-SREF links started working in mobile navigation!! Tks!

Bootstrap.navbar + Angular.routeProvider not working also not giving any error in chrome

I have posted configuration for routeProvider and navigation for it using bootstrap nav-bar but its not working also
I don't get any error in google-chrome, url http://localhost:8080/collegeCap/#!/#collegeCap replaces with corresponding href but doesn't display content of the page.
var CollegeCapApp = angular.module('CollegeCapApp',
['ngAnimate','ngSanitize', 'ui.bootstrap','ngRoute']);
CollegeCapApp.config(function($routeProvider){
$routeProvider
.when('/',{
templateUrl : 'resources/pages/collegeCap.jsp'
})
.when('/collegeCap',{
templateUrl : 'resources/pages/collegeCap.jsp'
})
.when('/collegeCapEvent',{
templateUrl : 'resources/pages/collegeCapEvent.jsp'
})
.when('/collegeCapLogin',{
templateUrl : 'resources/pages/collegeCapLogin.jsp'
})
.when('/collegeCapSignUp',{
templateUrl: 'resources/pages/collegeCapSignUp.jsp'
});
});
<div>
<nav class="navbar navbar-inverse">
<div class="navbar-header">
<button type="button"
class="navbar-toggle"
data-toggle="collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
CollegeCapApplication
</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>
CollegeCap
</li>
<li>
CollegeCapEvent
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="#collegeCapSignUp" class="navbar-link">
<span class="glyphicon glyphicon-user"></span> Sign up
</a>
</li>
<li>
<a href="#collegeCapLogin" class="navbar-link">
<span class="glyphicon glyphicon-login"></span> Login
</a>
</li>
</ul>
</div>
</nav>
</div>
~
With your current set up you're asking angular to find a route named #collegeCap (which does not exist). Change your links to use #!/routeName e.g.:
CollegeCap
This tells angular that the part after the #!/ should be processed by it's routing system and allow it to pick up the correct route.
Another fix is to reset the hash prefix back to the empty string by injecting $locationProvider into your module's config block and then setting the hash prefix as follows:
CollegeCapApp.config(function($routeProvider, $locationProvider){
// Add this line
$locationProvider.hashPrefix('');
$routeProvider
.when('/',{
templateUrl : 'resources/pages/collegeCap.jsp'
})
.when('/collegeCap',{
templateUrl : 'resources/pages/collegeCap.jsp'
})
.when('/collegeCapEvent',{
templateUrl : 'resources/pages/collegeCapEvent.jsp'
})
.when('/collegeCapLogin',{
templateUrl : 'resources/pages/collegeCapLogin.jsp'
})
.when('/collegeCapSignUp',{
templateUrl: 'resources/pages/collegeCapSignUp.jsp'
});
});
and then your link would become:
CollegeCap

UI-Router 1.0.0-beta.2 and UI-Bootstrap 2.1.4 dropdown not closing

I'm using ui-route ui-sref in my links with the navbar using UI-Bootstrap dropdowns and if I use the anchor tag with href, the dropdown closes on click, but the anchor tags with ui-sref does not close the dropdown. I've got the following plnkr with it showing the bug.
Angular Code
angular.module('app', ['ui.bootstrap', 'ui.router'])
.config(appRoute)
.controller('myController', myController);
appRoute.$inject = ['$stateProvider'];
function appRoute($stateProvider) {
$stateProvider.state('index', {
url: '/index',
template: '<h1>indexRoute</h1>'
})
.state('other', {
url: '/other',
template: '<h1>otherRoute</h1>'
})
}
function myController() {
var ctrl = this;
ctrl.title = 'Title';
ctrl.user = 'User#user.com';
ctrl.test = test;
ctrl.test2 = test2;
function test() {
console.log('TEST LINK!')
}
function test2() {
console.log('TEST 2')
}
}
HTML
<body ng-controller="myController as $ctrl" style="{padding-top: 70px;}">
<nav id="isec-menu" class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<div class="intelisecure-header">
<span>{{$ctrl.title}}</span>
</div>
</div>
<div class="collapse navbar-collapse navbar-right">
<ul class="nav navbar-nav">
<li id="usermenu" class="dropdown user-menu-dropdown" uib-dropdown="">
<a href="" class="uib-dropdown-toggle username" uib-dropdown-toggle="">
<i class="fa fa-user fa-fw"></i>
{{$ctrl.user}} <span class="caret"></span>
</a>
<ul class="dropdown-menu" uib-dropdown-menu="" role="menu">
<li role="menuitem">
<a href="/index" ng-click="$ctrl.test()">
<i class="fa fa-fw fa-sign-out menu-icon"></i>
<span class="menu-text">index href</span>
</a>
</li>
<li role="menuitem">
<a ui-sref="other" ng-click="$ctrl.test2()">
<i class="fa fa-fw fa-sign-out menu-icon"></i>
<span class="menu-text">other sref</span>
</a>
</li>
<li role="menuitem">
<a href="" ng-click="$ctrl.logout()">
<i class="fa fa-fw fa-sign-out menu-icon"></i>
<span class="menu-text">Logout</span>
</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<ui-view>
<div>
test
</div>
</ui-view>
</body>
Not perfectly built, I was trying to see if I could replicate the issue with my
https://plnkr.co/edit/Rz3AbgnYnWI34DjuByY6?p=preview
Apparently this was a known issue and was resolved by updating to UI-Router 1.0.0-beta.3

Scope variables don't reflect in template

I have part of an angular app setup as follows:
<section >
<div ng-controller="setHeaderCtrl" class="navbar navbar-default header">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapsed-bar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand brand-text" ui-sref="index">
<span class="sports-text">Sports</span>
<span class="gully-text">Gully</span>
</a>
</div>
<div class="collapse navbar-collapse" id="collapsed-bar">
<ul class="nav navbar-nav navbar-left" ng-if="searchingAway">
<li class="local-search-cont">
<input type="text" class="local-type-top" placeholder="Search">
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li ng-if="!userObject">
<a ng-click="showLogin()" class="navbar-txt-block" style="cursor: pointer;">
Login
</a>
</li>
<li ng-if="userObject.user">
<a class="navbar-txt-block" ui-sref="home" style="cursor: pointer;">
Home
</a>
</li>
<li ng-if="userObject">
{{userObject}}
<a class="navbar-txt-block" style="cursor: pointer;">
{{userDetails.first_name}} {{userDetails.last_name}}
</a>
</li>
</ul>
</div>
</div>
</div>
</section>
and my setHeaderCtrl is as follows:
app.controller('setHeaderCtrl',['$scope', '$rootScope', '$http', '$state', 'Page', '$modal',
function($scope, $rootScope, $http, $state, Page, $modal){
$rootScope.$on('userSet', function(){
var authKey = $rootScope.authKey;
$http.defaults.headers.common.Authorization = 'Token '+authKey;
$http.get('/loginCheck/').success(function(data){
$scope.userObject = data;
$scope.userDetails = data.user;
});
});
$scope.userObject = {}
$http.get('/loginCheck/').success(function(data){
$scope.userObject.user = data;
$scope.userDetails = data.user;
console.log($scope.userDetails);
}).error(function(data, status){
if(status == 401){
$scope.userObject.user = undefined;
}
});
$rootScope.$on('setAwayHeader', function(){
$scope.searchingAway = true;
});
$rootScope.$on('setIndexHeader', function(){
$scope.searchingAway = false;
});
$scope.showLogin = function(){
$modal.open({
windowClass: 'modal fade login-modal',
templateUrl: '/static/partials/modals/loginModal.html',
controller: 'loginCtrl'
})
}
}
])
My problem is such that the {{userObject}} or {{userDetails}} never displays. I can guarantee that there's a response from the server because console.log shows the correct data.
The expression ng-if="userObject" is not good, it always true. Replace it by boolean, or by userObject property: ng-if="userObject.user". Jsfiddle

Resources