When I use ngRouter, navbar is not collapsing - html

When I use ngRouter, navbar is not collapsing after selecting a link in small devices
angilarjs:
var app = angular.module('MyTemplate',['ngRoute','ui.bootstrap']);
app.controller("templateCtrl",function($scope){
$scope.navbarCollapsed = true;
});
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/Page1', {
templateUrl: 'Page1.html',
controller: 'Page1Ctrl'
})
.when('/Page2', {
templateUrl: 'Page2.html',
controller: 'Page2Ctrl'
})
.otherwise({
redirectTo: '/Page1'
});
}]);
HTML:
<div class="navbar-header">
<button type="button" class="navbar-toggle" ng-click="navbarCollapsed = !navbarCollapsed">
<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" href="">Navigation bar</a>
</div>
<!-- nav links for toggling -->
<div class="collapse navbar-collapse" collapse="navbarCollapsed">
<ul class="nav navbar-nav nav_width">
<li>Page1</li>
<li>Page2</li>
</ul>
</div>
Please help to fix this issue. Thank you...

Change the code like this:
var app = angular.module('MyTemplate',['ngRoute','ui.bootstrap']);
app.controller("templateCtrl",function($scope){
$scope.navbarCollapsed = true;
$scope.collapseNavbar = function () {
$scope.navbarCollapsed = true;
};
});
And the html
<div class="collapse navbar-collapse" collapse="navbarCollapsed">
<ul class="nav navbar-nav nav_width">
<li>Page1</li>
<li>Page2</li>
</ul>
</div>
It's adding a function to the scope to force the collapse property to true

Related

collapsible navbar will not work (using bootstrap)

I have been working with this for awhile. The navbar shows up just fine, but the functionality is not there. Here is the code:
<nav class = "navbar navbar-inverse navbar-fixed-top" role = "navigation" id = "my-navbar">
<div class = "container">
<div class = "navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-ex1-collapse" aria-expanded="false">
<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" href = "index.html">WM</a>
</div>
<div class = "collapse navbar-collapse" id = "myNavbar">
<ul class = "nav navbar-nav navbar-right">
<li>Projects</li>
<li>Resume</li>
<li>Contact Me</li>
<input type = "button" class="btn btn-info navbar-btn" value = "Blog" onclick = "relocate_blog()" id = "blog-button">
<script>
function relocate_blog() {
location.href = "blog.html ";
}
</script>
</ul>
</div>
</div>
<!--End Container -->
</nav>
<!-- End Navbar -->
<nav class = "navbar navbar-inverse navbar-fixed-top" role = "navigation">
<div class = "container">
<div class = "navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar" aria-expanded="false">
<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" href = "index.html">WM</a>
</div>
<div class = "collapse navbar-collapse" id = "myNavbar">
<ul class = "nav navbar-nav navbar-right">
<li>Projects</li>
<li>Resume</li>
<li>Contact Me</li>
<input type = "button" class="btn btn-info navbar-btn" value = "Blog" onclick = "relocate_blog()" id = "blog-button">
<script>
function relocate_blog() {
location.href = "blog.html ";
}
</script>
</ul>
</div>
</div><!--End Container -->
</nav><!-- End Navbar -->

Bootstrap nav wouldn't close

I know that there are other posts with the same subject, but I tried a lot of them but still got the problem.
I can perfectly open the nav, but when I want to close it, it just flicker, and it doesn't close at all.
<nav class="navbar navbar-default">
#*navbar navbar-default*#
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<p class="hidden-sm hidden-md hidden-lg">#Model.Content.Name</p>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="menu-collapse">
<ul class="nav navbar-nav">
#foreach (var item in selection)
{
if (item.Children.Count() > 0)
{
<li class="dropdown #(item.Id == Model.Content.Id ? "current" : null)">
#item.Name.ToUpper()
<ul class="dropdown-menu">
#foreach (var child in item.Children)
{
<li class="#(item.Id == Model.Content.Id ? "current" : null)">
#child.Name.ToUpper()
</li>
}
</ul>
</li>
}
else
{
<li class="#(item.Id == Model.Content.Id ? "current" : null)">
#item.Name.ToUpper()
</li>
}
}
</ul>
</div><!-- /.navbar-collapse -->
Html.RequiresCss("/css/bootstrap.min.css", 1);
Html.RequiresCss("/css/bootstrap-responsive.css", 1);
Html.RequiresJs("https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js", 0);
Html.RequiresJs("/Scripts/bootstrap.min.js", 1);
I hope that someone know what can cause this error.

how can I change the background after scroll

I have a navbar with background transparent. How can make background black after scrol.
Here is template live
and there is html with navbar
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand"><img src="img/logo.svg" class="brand"></a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li>Despre noi</li>
<li>Servicii</li>
<li>Galerie</li>
<li>Contact</li>
</ul>
</div>
</div>
</nav>
Do it with Jquery and add a class
jQuery
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 1) { // Adjust with your needs
$(".navbar").addClass("navbar-black");
} else {
$(".navbar").removeClass("navbar-black");
}
});
CSS
.navbar-black{
background: #000 !important;
}
so you should be able to achieve this with ScrollTop,
Something like:
onscroll = function()
{
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > 830)
{
$('.navbar').css('background-color','black');
}
if (scrollTop < 830)
{
$('.navbar').css('background-color','transparent');
}
}
I would perhaps consider using an anchor rather than an absolute height.
If this is what you wanted. :)

href not working not moving page to linked file

i used href to link file used this
`
when user clicks on logo user will be redirected to link but in my case its not working.
Also tried:
can someone help what's wrong in this you can also live demo here http://www.xpoter.in/blog/ just click on logo you can find error
my head tag
<div class="navbar-wrapper">
<div class="container">
<div class="navbar navbar-default navbar-fixed-top" role="navigation" id="top-nav">
<div class="container">
<div class="navbar-header">
<!-- Logo Starts -->
<!-- #Logo Ends -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Nav Starts -->
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right scroll">
<li class="active">Home</li>
<li >About</li>
<li >Partners</li>
<li >Contact</li>
</ul>
</div>
<!-- #Nav Ends -->
</div>
</div>
</div>
</div>
The problem is a piece of jQuery on your page:
jQuery(document).ready(function ($) {
$(".scroll a, .navbar-brand, .gototop").click(function (event) {
event.preventDefault();
$('html,body').animate({scrollTop: $(this.hash).offset().top}, 600, 'swing');
$(".scroll li").removeClass('active');
$(this).parents('li').toggleClass('active');
});
});
Which fires on click of .navbar-brand, and you are using event.preventDefault() which is preventing your link from working

Navbar collapsed is closing while clicking on dropdown-toggle

When the navbar is collapsed, I can't click on the elements on the dropdowns because when I click on a dropdown, it's closing the navbar. How can I avoid this behavior ?
<nav ng-app="app" ng-controller="AppCtrl" class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" ng-init="navCollapsed = true" ng-click="navCollapsed = !navCollapsed">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" collapse="navCollapsed" ng-click="navCollapsed=true">
<ul class="nav navbar-nav pull-right">
<li id="home-tab" ng-class="{active : isActive('home-tab')}" ng-click="activateTab('home-tab')" class="tab"><span class="glyphicon glyphicon-home"></span>Home</li>
<li id="stats-tab" ng-class="{active : isActive('stats-tab')}" ng-click="activateTab('stats-tab')" class="tab"><span class="glyphicon glyphicon-stats"></span>Stats</li>
<li id="administration-tab" ng-class="{active : isActive('administration-tab')}" ng-click="activateTab('administration-tab')" class="tab dropdown"><a class="dropdown-toggle"><span class="glyphicon glyphicon-book"></span>Administration<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Content</li>
<li>Terminal</li>
<li>Users</li>
</ul>
</li>
<li id="profile-tab" ng-class="{active : isActive('profile-tab')}" ng-click="activateTab('profile-tab')" class="tab dropdown"><a data-toggle="dropdown" class="dropdown-toggle"><span class="glyphicon glyphicon-user"></span>Profile<span class="caret"></a>
<ul class="dropdown-menu">
<li>Me</li>
<li>Company</li>
</ul>
</li>
<li id="logout-tab" class="tab"><a><span class="glyphicon glyphicon-off"></span>Logout</a></li>
</ul>
</div>
</div>
</nav>
JSFiddle
Note: In the JSFiddle example, when you click on a dropdown, it is not expending. I don't know why, anyway it is working in my code. Forget that and look at the issue that makes my collapsed navbar closing while clickng on a dropdown. ;)
It's because you have
ng-click="navCollapsed=true"
on the main navbar div
<div class="collapse navbar-collapse" collapse="navCollapsed" ng-click="navCollapsed=true">
which means that the navbar closes anywhere you click on it, not just on the tabs.
Here is a version that doesn't collapse the navbar when you click on 'Administration' - Fiddle
<div class="collapse navbar-collapse" collapse="navCollapsed">
New activateTab:
$scope.activateTab = function(tabId) {
if (tabId != 'administration-tab') {
$scope.navCollapsed = true;
}
$scope.activeTab = tabId;
};