How to separate menu into different view in AngularJS ui-router application? - angularjs

I have this:
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<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" ui-sref="main">Project name</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li ui-sref-active="active"><a ui-sref="main.tm">Task manager</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li ui-sref-active="active"><a ui-sref="main.settings">Settings</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<!-- Main component for a primary marketing message or call to action -->
<div class="jumbotron" ui-view>
<div class="main-viewer" ></div>
</div>
</div> <!-- /container -->
Here is how states conf:
$stateProvider.state({
name: 'main',
url: '/',
template: mainTemplate
});
$stateProvider.state({
name: 'main.tm',
url: '/tm',
controller: 'tm.list',
template: view
});
$stateProvider.state({
name: 'main.settings',
url: '/settings',
controller: 'settings.ctrl',
template: view
});
$urlRouterProvider.otherwise('/tm');
Summary:
Now I have header (menu) and content block. Dependent on what button user press in menu settings or tm module appears in content block. Both settings and tm module is in separated directories (different modules).
The problem:
Menu html and container for settings and tm block (<div ui-view></div>) are in one HTML file. I want to separate menu into different module (like settings and tm are in separated modules).
What I want:
main module will be like (view pseudocode):
<container for menu>
<container for content>
Menu will be in directory menu and there will be files menu.html and menu.ctrl.js. It will be "injected" to container "container for menu". tm and settings already in correct directories and have their own views and controllers.
Whole code is here.
PS. One of the expected results was:
<menu-component></menu-component>
<div class="container">
<!-- Main component for a primary marketing message or call to action -->
<div class="jumbotron" ui-view>
<div class="main-viewer">
Welcome page
</div>
</div>
</div> <!-- /container -->
Now menu logic is in the separated controller and view is in the separated html.

Related

Angular Routing and Bootstrap Navbar

I have problems getting the bootstrap navbar + angular routing to work. I don't understand what is going on since a very similar code on another app is working.
I have the following html in my index.html
<div ng-controller="CoreController as core">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- 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="#bs-example-navbar-collapse-1" 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="#">myApplication</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li ng-class="core.navHome">Home <span class="sr-only">(current)</span></li>
<li ng-class="core.navStatistic">Statistic</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<!-- angular templating -->
<!-- this is where content will be injected -->
<div ng-view></div>
</div>
my app.js:
$routeProvider.when('/', { templateUrl: 'templates/home.html', controller: 'MainController', controllerAs: 'main' });
$routeProvider.when('/statistic', { templateUrl: 'templates/statistic.html', controller: 'StatisticController', controllerAs: 'statistic' });
// Default route
$routeProvider.otherwise({ redirectTo: '/' });
When I hit the statistic link, my URL shows:
http://localhost/myApplication/#!/#statistic
and I get see the home template.
The home button then shows "http://localhost/myApplication/#!/"
Is the #! the reason for this?
Ok after some research I found out that I this code worked for angular < 1.6.
To make the above code work in 1.6 you have to get ridd of the #! by adding this to the config:
$locationProvider.hashPrefix('');
Here is the original Post.

Rendering Angularjs specfic view without header and footer

So all of my pages use the index.html to render pages and in the index.html, my code is structured so that all views render in the div with the ng-view="" tag as:
<body>
<div class="navbar navbar-inverse" role="navigation" ng-controller="indexController" style="border-bottom: #ab2328 3px solid;">
<div class="container">
<div class="navbar-header">
<button class="btn btn-success navbar-toggle" ng-click="navbarExpanded = !navbarExpanded">
<span class="glyphicon glyphicon-chevron-down"></span>
</button>
<a ng-show="!isSWA" class="navbar-brand" href="http://www.naviabenefits.com" target="_blank"><img style="height: 37px;" src="ppt/assets/navia.png"></a>
<a ng-show="isSWA" class="navbar-brand" href="http://pebb.naviabenefits.com/" target="_blank"><img style="height: 37px;" src="ppt/assets/navia.png"></a>
</div>
<div class="collapse navbar-collapse" collapse="!navbarExpanded">
<ul ng-show="!isSWA" class="nav navbar-nav navbar-right">
<li ng-show="authentication.isAdmin">admin</li>
<li>products + services</li>
<li>about us</li>`
<li>testimonials</li>
<li>careers</li>
<li>news</li>
<li>blog</li>
<li>contact us</li>
<li ng-show="authentication.isAuth"><a id="signOut" href="#/logout" >sign out <i class="fa fa-sign-out"></i></a></li>
</ul>
</div>
</div>
</div>
<div class="container-fluid">
<div ng-view="">
</div>
</div>
<div id="footer" role="navigation" ng-controller="indexController">
<div class="container">
<div class="footerLinks">
</div>
</div>
</div>
</body>
So I have a couple of pages that I want to render without the header and footer, but still keep some of the other pathing.
This "plain" page has its own controller and all and my routing for it in my app.js looks like:
$routeProvider.when("/claimReceipt-plain", {
controller: "claimReceiptController",
templateUrl: "ppt/views/claims/claimReceipt-plain.html"
});
How can I do this within this index.html and not have the header and footers render, but me more or less, its own page?
Define a scope level Boolean variable on the page controller where you don't want the header and use ng-show directive to apply condition on the header div tag.

Bootstrap: collapsable navbar doesn't work

I have a webapp built with yeoman gulp-angular generator that uses bootstrap (with sass).
I have a collapsable navbar. When I click on the button with the 3 icon-bars, the menu is not displayed as expected.
I've tested my html apart and it works fine. Fiddle that works is: http://jsfiddle.net/7tzj3y5n/.
Code is:
<nav class="navbar navbar-default navbar-inverse navbar-static-top navbar-inverse-main" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navigationbar">
<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="navbar-brand">Merchandising Control</p>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-menu" id="navigationbar">
<ul class="nav navbar-nav navbar-right">
<!-- Airline code label -->
<li>Airline:</li>
<!-- Airline code dropdown -->
<li class="dropdown" dropdown>
<a class="dropdown-toggle" dropdown-toggle>{{MainCtrl.getAirlineCode()}} <span class="caret"></span></a>
<ul class="dropdown-menu dropdown-menu-main">
<li ng-repeat="airlineCode in MainCtrl.MAIN_UI_CONSTANTS().airlineCodes" ng-click="MainCtrl.setAirlineCode(airlineCode)">
<a>{{airlineCode.value}}</a>
</li>
</ul>
</li>
</ul>
</div> <!-- /.navbar-collapse -->
</div> <!-- /.container-fluid -->
</nav>
It seems that my installation is not correct.
Could someone help me ?
Regards.
While Bootstrap's js uses data-* attributes on your HTML elements to enable expand / collapse on the navbar, Angular UI Bootstrap (which is what you should be using instead of Bootstrap's js when you have an Angular application) does not work exactly the same way. You can see Angular UI Bootstrap's documentation on Collapse to see how it works, however what you should be doing in short is the following:
Remove Boostrap's js and include Angular UI Bootstrap js an add 'ui.bootstrap' as a dependency of your Angular module (if you haven't already).
Update your HTML to use Angular UI Bootstrap's collapse, ie. something like the following:
<nav class="navbar navbar-default navbar-inverse navbar-static-top navbar-inverse-main" role="navigation" ng-init="navCollapsed = true" >
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" ng-click="navCollapsed = !navCollapsed">
<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="navbar-brand">Merchandising Control</p>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-menu" collapse="navCollapsed">
<ul class="nav navbar-nav navbar-right">
<!-- Airline code label -->
<li>Airline:</li>
<!-- Airline code dropdown -->
<li class="dropdown" dropdown>
<a class="dropdown-toggle" dropdown-toggle>{{MainCtrl.getAirlineCode()}} <span class="caret"></span></a>
<ul class="dropdown-menu dropdown-menu-main">
<li ng-repeat="airlineCode in MainCtrl.MAIN_UI_CONSTANTS().airlineCodes" ng-click="MainCtrl.setAirlineCode(airlineCode)">
<a>{{airlineCode.value}}</a>
</li>
</ul>
</li>
</ul>
</div> <!-- /.navbar-collapse -->
</div> <!-- /.container-fluid -->
</nav>
What this code actually does is use the navCollapsed angular variable to control the menu's expand / collapse.

Angular switching CSS class based on route?

I have the below HTML file:
<header ng-include="'app/partials/includes/header.html'"></header>
<div class="container" ng-controller="LoginCtrl">
</div>
The above is for the login page, the below is for the register:
<header ng-include="'app/partials/includes/header.html'"></header>
<div class="container" ng-controller="RegisterCtrl">
</div>
This is the header:
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<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>
Curve
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active">Sign Up</li>
<li>Sign In</li>
</ul>
</div>
</div>
</div>
How can apply the .active class to the list element dependent upon the route?
If the route is /register, I want the active class applied, and same for login.
How can this be done in Angular?
Use ng-class on any element to conditionally apply to an element. Using the object notation the key is the class name, and the value is an expression when evaluates to true the class is added to that element's class attribute.
<div class="header" ng-controller="HeaderController">
<div ng-class="{ active: registrationOrLogin }">
</div>
And in a controller:
app.controller("HeaderController", [ "$scope", "$location", function($scope, $location) {
$scope.registrationOrLogin = $location.hash() == "/login" || $location.hash() == "/registration";
} ] );

Logout approach for angularjs?

I have started learning angularjs for two days, there a lot of new concepts that are new to me, i'm building a test project to my final project testing all the new different things so far,
I'm stuck in the logout, i have implement the ng-if directive in the login controller and it works great i have test it the success method of login request, but i want to move my logout html to the navbar which is in the index.html, so i'm not sure where to put my logic to test against the cookie ?
that's my index.html
<div ng-controller="HomeController">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<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="/">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Register</li>
<li>Login</li>
<li>Articles</li>
<li ng-if ="isAuth" ng-click="LogOut()">LogOut</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
<div data-ng-view=""></div>
HomeController
app.controller('HomeController', function ($scope, Auth) {
$scope.isAuth = Auth.IsAuth;
});
Auth service
app.service('Auth', function ($cookieStore) {
var auth = function myfunction() {
if ($cookieStore.get('token')) {
return true;
}else{
return false;
}
}
return{
isAuth : auth()
}
});

Resources