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
Related
I am having a website which was created using bootstrap. now i am migrating my project to react. So, I was trying to use react-bootstrap. can someone help me to translate this Nav bar code written in bootstrap to react-bootstrap. I have tried changing class to className but it didnt work.
<nav class="navbar navbar-expand-lg navbar-dark ftco_navbar bg-dark ftco-navbar-light" id="ftco-navbar">
<div class="container">
<a class="navbar-brand" href="/index"><img src="{% static 'lab/images/logos/newlogo.png' %}" /></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#ftco-nav"
aria-controls="ftco-nav" aria-expanded="false" aria-label="Toggle navigation">
<span class="oi oi-menu"></span> Menu
</button>
<div class="collapse navbar-collapse" id="ftco-nav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active"><a href="/index" class="nav-link"
onMouseOver="this.style.color='#A41034'" onmouseout="this.style.color='black'"
style="text-transform: capitalize;">Home</a></li>
<li class="nav-item active"><a href="/research" class="nav-link"
onMouseOver="this.style.color='#A41034'" onmouseout="this.style.color='black'"
style="text-transform: capitalize;">Research</a></li>
<li class="nav-item active"><a href="/publications" class="nav-link"
onMouseOver="this.style.color='#A41034'" onmouseout="this.style.color='black'"
style="text-transform: capitalize;">Publications</a></li>
<li class="nav-item active dropdown">
<a class="nav-link dropdown-toggle" href="/team" id="navbarDropdown" role="button"
onMouseOver="this.style.color='#A41034'" onmouseout="this.style.color='black'"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
style="text-transform: capitalize;">
People
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/pi" onMouseOver="this.style.color='#A41034'"
onmouseout="this.style.color='black'">Principal Investigators</a>
<a class="dropdown-item" href="/researchers"
onMouseOver="this.style.color='#A41034'"
onmouseout="this.style.color='black'">Senior Scientists</a>
<a class="dropdown-item" href="/phd_students"
onMouseOver="this.style.color='#A41034'" onmouseout="this.style.color='black'">PhD/Masters
Students</a>
<a class="dropdown-item" href="/students" onMouseOver="this.style.color='#A41034'"
onmouseout="this.style.color='black'">Research Trainees</a>
<a class="dropdown-item" href="/alumni" onMouseOver="this.style.color='#A41034'"
onmouseout="this.style.color='black'">Alumni</a>
</div>
</li>
<li class="nav-item active"><a href="/contact" class="nav-link"
onMouseOver="this.style.color='#A41034'" onmouseout="this.style.color='black'"
style="text-transform: capitalize;">Projects</a></li>
<li class="nav-item active"><a href="/joinus" class="nav-link"
onMouseOver="this.style.color='#A41034'" onmouseout="this.style.color='black'"
style="text-transform: capitalize;">Join
Us</a></li>
</ul>
</div>
</div>
</nav>
program the react extension class to render as React components then program the react bootstrap in another file exporting the navbar CSS as props to app return, make sure you import bootstrap
Can you please make sure you include bootstrap CDN or install bootstrap properly?
And also make sure you import bootstrap.
According to your code, it should work.
I am trying to use the Header component of material design bootstrap in my React Application. And I pasted the below line of code from official website of Material design bootstrap to implement Header collapsible when screen size is small but it. Anyone have any idea what else need to do in order to make it work in react. I just paste it inside my react component. There is some kind of state I need to maintain here.
<header>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-white">
<div class="container-fluid">
<button
class="navbar-toggler"
type="button"
data-mdb-toggle="collapse"
data-mdb-target="#navbarExample01"
aria-controls="navbarExample01"
aria-expanded="false"
aria-label="Toggle navigation"
>
<i class="fas fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarExample01">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item active">
<a class="nav-link" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Navbar -->
<!-- Jumbotron -->
<div class="p-5 text-center bg-light">
<h1 class="mb-3">Heading</h1>
<h4 class="mb-3">Subheading</h4>
<a class="btn btn-primary" href="" role="button">Call to action</a>
</div>
<!-- Jumbotron -->
</header>
I have a menu on website. Below is the HTML code.
<header class="navbar navbar-sticky navbar-expand-lg navbar-dark">
<div class="container position-relative">
<a class="navbar-brand" href="./index.html">
<img class="navbar-brand-regular" src="logo.png"/>
<img class="navbar-brand-sticky" src="logo.png"/>
</a>
<button class="navbar-toggler d-lg-none" type="button" data-toggle="navbarToggler" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-inner">
<!-- Mobile Menu Toggler -->
<button class="navbar-toggler d-lg-none" type="button" data-toggle="navbarToggler" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<nav>
<ul class="navbar-nav" id="navbar-nav">
<li class="nav-item">
<a class="nav-link scroll" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link scroll" href="#about">About</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
On Mobile menu, when i click on a menu item, the menu did not closed. Below is the script code.
Did I miss anything?
// MAIN MENU TOGGLER ICON (MOBILE SITE ONLY)
$('[data-toggle="navbarToggler"]').click(function () {
$('.navbar').toggleClass('active');
$('body').toggleClass('canvas-open');
});
// MAIN MENU TOGGLER ICON
$('.navbar-toggler').click(function () {
$('.navbar-toggler-icon').toggleClass('active');
});
Bootstrap will handle all toggle/collapse by itself you do not need to write extra JS. Toggle button must not duplicate as you did to avoid messing up the navbar functioning.
Below is the answer. I optimized your params in few places. If my answer works for you, please tick it as final answer and upvote it so other people with same problem will get help too. Cheers
<nav class="navbar navbar-dark navbar-expand-sm fixed-top">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#Navbar">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand mr-auto" href="./index.html"> <img src="#" height="#" width="#"> </a>
<div class="collapse navbar-collapse" id="Navbar">
<ul class="navbar-nav mr-auto">
<li class="navbar-item active"><a class="nav-link" href="./index.html"> Home</a></li>
<li class="navbar-item"><a class="nav-link" href="./aboutus.html">About</a></li>
</ul>
</div>
</div>
</nav>
I'm new to Angular and still learning. I've created 'header.html' file and include it in my other html files using 'ng-include'. inside my 'header.html', I have a nav bar and I want nav bar links active with the active page. i tried hard and still couldn't get done. much appreciate if someone could help me to figure this out. thanks.
header.html
<body ng-app="headerModule">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup" ng-controller="headerContr">
<div class="navbar-nav">
<a class="nav-item nav-link ng-class:{ active: isActive('/index.html')};" href="./index.html"><span class="fa fa-home fa-lg"></span> Home</span></a>
<a class="nav-item nav-link ng-class:{ active: isActive('/aboutus.html')};" href="./aboutus.html"><span class="fa fa-info fa-lg"></span> About Us</a>
<a class="nav-item nav-link ng-class:{active: isActive('/contactus.html')};" href="./contactus.html"><span class="fa fa-address-book fa-lg"></span> Contact us</a>
</div>
</div>
</nav>
<header class="jumbotron jumbotron-fluid">
<div class="container fluid">
<div class="row">
</div>
</div>
</header>
app.js
var angApp = angular.module('angApp', []);
var headerModule = angular.module('headerModule', []);
headerModule.controller('headerContr', ['$scope', '$location', function($scope, $location){
$scope.isActive = function (viewLocation) {
return viewLocation === $location.path();
};
}]);
There are two ways to use ng-class in the HTML:
As an attribute - <p ng-class='{"active" : isActive(params)}'></p>
As CSS class- <p class='ng-class: isActive(params)'></p>
The first evlas the expression and sets the class active if true.
The second simply takes the returned value of the expression and sets it as a class, so if isActive() returnes active, that class would be used.
NgClass Documentation
This is a valid method for NgClass. It may be because you have added the {} into the ng-class: definition as well as passing more than an expression.
Try removing the {} from each instance like so and updating the isActive method to return active (the active class) if true and '' (blank) if false:
<div class="navbar-nav">
<a class="nav-item nav-link ng-class: isActive('/index.html')" href="./index.html"><span class="fa fa-home fa-lg"></span> Home</span></a>
<a class="nav-item nav-link ng-class: isActive('/aboutus.html')" href="./aboutus.html"><span class="fa fa-info fa-lg"></span> About Us</a>
<a class="nav-item nav-link ng-class: isActive('/contactus.html')" href="./contactus.html"><span class="fa fa-address-book fa-lg"></span> Contact us</a>
</div>
$scope.isActive = function (viewLocation) {
if(viewLocation === $location.path()) return 'active';
else return '';
};
OTHER OPTION:
I recommend using the attribute Usage of NgClass. Try to pull it out of the class attribute and use it as it's own attribute.
<body ng-app="headerModule">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup" ng-controller="headerContr">
<div class="navbar-nav">
<a class="nav-item nav-link" ng-class="{ active: isActive('/index.html')}" href="./index.html"><span class="fa fa-home fa-lg"></span> Home</span></a>
<a class="nav-item nav-link" ng-class="{ active: isActive('/aboutus.html')}" href="./aboutus.html"><span class="fa fa-info fa-lg"></span> About Us</a>
<a class="nav-item nav-link" ng-class="{active: isActive('/contactus.html')}" href="./contactus.html"><span class="fa fa-address-book fa-lg"></span> Contact us</a>
</div>
</div>
</nav>
<header class="jumbotron jumbotron-fluid">
<div class="container fluid">
<div class="row">
</div>
</div>
</header>
I think you have problem with syntax:
<a class="nav-item nav-link ng-class:{ active: isActive('/index.html')};" href="./index.html"><span class="fa fa-home fa-lg"></span> Home</span></a>
ng-class must be attribute, not embedded in class attribute .
<a class="nav-item nav-link" ng-class="{ active: isActive('/index.html')};" href="./index.html"><span class="fa fa-home fa-lg"></span> Home</span></a>
Navbar-toggle button was on the top-right both web and mobile mode, but when the navbar collapsed on mobile mode something gone wrong (last pic).
I know it's because my code's line order, but I don't know how to fix it.
Web mode:
Mobile Mode:
Collapsed mobile mode:
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">EVENT</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?=$_SESSION['name'];?>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<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 class="dropdown-divider"></div>
<a class="dropdown-item" href="login/logout.php">Logout</a>
</div>
</li>
</ul>
</div>
<div class="nav-item" style="float:top">
<input class="btn btn-danger" type="submit" value="Create">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
One way to solve this problem would be:
Put the code for the red button after the navbar brand item in the HTML
Give it the classes ml-auto order-lg-last. The former means "margin-left:auto" and the latter will reorder that button on screens that are large (lg) or larger and put it in the last position. ml-auto will then actually push it to the right.
Give the toggler (that should come after the red button in the HTML) the ml-2 class to get a bit of space between it and the red button.
Click "run code snippet" button below and expand to full page:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">EVENT</a>
<div class="nav-item ml-auto order-lg-last">
<input class="btn btn-danger" type="submit" value="Create">
</div>
<button class="navbar-toggler ml-2" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?=$_SESSION['name'];?>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<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 class="dropdown-divider"></div>
<a class="dropdown-item" href="login/logout.php">Logout</a>
</div>
</li>
</ul>
</div>
</nav>