ui-router loggin page without menu - angularjs

I'm a begginer with angular 1.6 and I would like to do the next:
Loggin, register & remember password without menu.
After loggin: Menu, nav bar and footer appears.
Up to now I have the next:
Index.html
<ui-view></ui-view>
Loggin: Works fine.
authenticationService.Login(vm.email, vm.password, function (result) {
if (result === true) {
$location.path('/main');
} else {
vm.error = 'Username or password is incorrect.';
vm.loading = false;
}
});
};
If I have logged I go to main, here my problems appear..
<!-- Wrapper-->
<div id="wrapper">
<!-- Navigation -->
<div ng-include="'../views/partials/main-navigation.html'"></div>
<!-- Page wraper -->
<div id="page-wrapper">
<!-- Topnavbar -->
<div ng-include="'../views/partials/main-topnavbar.html'"></div>
hola mundo
<!-- Main view -->
<div ui-view>aqui tengo que meter todas las vistas</div>
<!-- Footer -->
<div ng-include="'../views/partials/main-footer.html'"></div>
</div>
<!-- End page wrapper-->
Now I would like to put the rest of my web pages inside the main
<div ui-view>All pages here</div>
How Can I do this?
I have read several pages https://ui-router.github.io/ and some more, but It is not clear for me.
I think I have to do some in my app.js but I don't now what...
(function () {
'use strict';
angular.module('frontEndApp', [
'ui.router',
'ngMessages',
'ngStorage',
'ngAnimate',
'ngAria',
'ngCookies',
'ngMessages',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'ngStorage',
'ngResource',
'pascalprecht.translate']).config(config).run(run);
function config ($stateProvider, $translateProvider, $urlRouterProvider) { // default route
$urlRouterProvider.otherwise('/login');// app routes
var states = [
{ name: 'main', url: '/main', templateUrl: 'views/main/main.html' },
{ name: 'login', url: '/login', templateUrl: 'views/login/login.html', controller: 'loginController', controllerAs: 'vm' },
{ name: 'department', url: '/department', templateUrl: 'views/department/department.html', controller: 'departmentController', controllerAs: 'vm' }
];
// Loop over the state definitions and register them
states.forEach(function (state) {
$stateProvider.state(state);
});
/**
* Translation of the web page with angular-Translate
*/
$translateProvider.useStaticFilesLoader({
files: [{
prefix: '../config/languages/locale-',
suffix: '.json'
}]
});
$translateProvider.preferredLanguage('en');
$translateProvider.useSanitizeValueStrategy('escapeParameters');
}
function run ($rootScope, $http, $location, $localStorage) { // keep user logged in after page refresh
if ($localStorage.currentUser) {
$http.defaults.headers.common.Authorization = 'Bearer ' + $localStorage.currentUser.token;
}
// redirect to login page if not logged in and trying to access a restricted page
$rootScope.$on('$locationChangeStart', function (event, next, current) {
var publicPages = ['/login'];
var restrictedPage = publicPages.indexOf($location.path()) === -1;
if (restrictedPage && !$localStorage.currentUser) {
$location.path('/login');
}
});
}
})();
Thanks in advance for your help.

Simply:
{ name: 'main', url: '/main', templateUrl: 'views/main/main.html' },
{ name: 'login', url: '/login', templateUrl: 'views/login/login.html',
controller: 'loginController', controllerAs: 'vm' },
{ name: 'main.department', url: '/department', templateUrl:
'views/department/department.html', controller: 'departmentController',
controllerAs: 'vm' }
If department belongs to main then call it: 'main.department'

Related

Named view dashboard not working with child view

i have defined my state like this below.
var app = angular.module('app', [
'ngAnimate',
'ngSanitize',
'ui.router',
'ui.bootstrap',
'ui.jq',
'abp'
]);
//Configuration for Angular UI routing.
app.config([
'$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/',
templateUrl: '/App/Main/views/home/home.cshtml',
menu: 'Home' //Matches to name of 'Home' menu in EMRNavigationProvider
})
.state('personview', {
url: '/person/view',
templateUrl: '/App/Main/views/person/view/index.cshtml',
views: {
"viewTop": { templateUrl: "/App/Main/views/person/view/viewTop.cshtml" },
"viewMain": { templateUrl: "/App/Main/views/person/view/viewMain.cshtml" },
"viewAllergies": { templateUrl: "/App/Main/views/person/view/allergies.cshtml" },
"viewAppointments": { templateUrl: "/App/Main/views/person/view/appointments.cshtml" },
"viewimmunization": { templateUrl: "/App/Main/views/person/view/immunization.cshtml" },
"viewNotes": { templateUrl: "/App/Main/views/person/view/notes.cshtml" },
},
menu: 'ViewPerson',
})
.state('personsearch', {
url: '/person/search',
templateUrl: '/App/Main/views/home/home.cshtml',
menu: 'SearchPerson'
})
;
}
]);
my index.cshtml for multiple named view looks like
<div class="right_col" role="main">
545465464646454545
<div ui-view="viewTop"></div>
<div ui-view="viewMain"></div>
<div class="row">
<div ui-view="viewAllergies"></div>
<div ui-view="viewAppointments"></div>
<div ui-view="viewimmunization"></div>
</div>
<div class="row">
<div ui-view="viewNotes"></div>
</div>
</div>
For some reason when i browse to http://myhost/#/person/view it gives me content for my home page and not the dashboard view (multiple named view) i was hoping with personview from above route.
if i remove property views from 'personview' named view it displays my hard coded 545465464646454545 correctly in screen and does not give content for home page.this tells me that having child views: under the route is not working.
what is wrong with route above for multiple named views that it does not like to render?
You must modify code like this:
Template index.cshtml must have tag <div ui-view="content"></div> and may include viewTop, viewMain
Template View.cshtml consists one of templates: viewAllergies, viewAppointments etc.
app.config([
'$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('person', {
abstract: true,
url: '/',
template:'<div ui-view=""></div>'
})
.state('person.view', {
url: '/personview',
views: {
'': {
templateUrl: '/App/Main/views/person/view/index.cshtml'
},
'content#person.view': {
templateUrl: '/App/Main/views/person/view/View.cshtml'
}
}
})
}
]);

How to pass data from a controller in templateUrl

This is the route specified in my file:
.state(
"path", {
url: "/{path:.*}",
views: {
"secondnav": {
templateUrl: "/App/View/Partials/_partDetails.html",
controller: 'unitDetailsController' },
"body": {
controller: 'unitDetailsController',
templateUrl: function($scope) {
return "/app/View/unitDetails.html";
}
}
}
})
How to pass data from controller unitDetailsController in templateUrl?
You can pass data like in the following code:
ui-router config:
angular
.module('angularApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'ui.router'
])
.config(function($stateProvider, $urlRouterProvider) {
//
// For any unmatched url, redirect to /state1
$urlRouterProvider.otherwise("/state1");
//
// Now set up the states
$stateProvider
.state('state1', {
url: "/state1/:id",
templateUrl: "views/partials/state1.html",
});
});
Template:
<div ng-controller="RowsCtrl">
<h1>State 1</h1>
<p>Id: {{id}}</p>
<div ui-view></div>
</div>

AngularJS Toaster doesn't work with state.go

I have a problem with this component . If I open the page directly , you see the warning. If I arrive at the page from another page do not.
ROUTER:
$stateProvider.state('login', {
url: '/login',
templateUrl: "assets/views/login.html",
resolve: loadSequence('modernizr', 'moment', 'sweet-alert', 'oitozero.ngSweetAlert', 'toaster'),
abstract: true
}).state('login.lockscreen', {
url: '/lock',
templateUrl: "assets/views/login_lock_screen.html",
title: 'Lock screen',
resolve: loadSequence('userLoginCtrl')
});
Factory for Alert:
'use strict';
app.factory("alertGenerate", ["toaster", function(toaster) {
return {
show: function(options) {
console.log("SHOW ALERT");
console.log(options);
/*
* TOASTER
*/
var config = {
type: options.type,
title: options.title,
body: options.text
};
if(options.confirmButtonText || options.showCloseButton) {
config.showCloseButton = true;
}
toaster.pop(config);
}
}
}]);
CONTROLLER:
app.controller('UserLoginCtrl', ["$rootScope", "$scope", "$auth", "$http", "$state", "$filter", "alertGenerate", function ($rootScope, $scope, $auth, $http, $state, $filter, alertGenerate) {
alertGenerate.show({
type: 'error',
title: 'Title text',
body: 'Body text',
showCloseButton: true,
closeHtml: '<button>Close</button>'
});
login.html (abstract)
<!-- toaster directive -->
<toaster-container toaster-options="{'position-class': 'toast-bottom-right', 'close-button':true}"></toaster-container>
<!-- / toaster directive -->
<div ui-view class="fade-in-right-big smooth"></div>
login_lock_screeen.html
<!-- start: LOCK SCREEN -->
<div class="row">
<div class="lock-screen">
hello
</div>
</div>

Separate nav from ng-view

I'm brand new to Angularjs and am trying to set up a new site but I'm confused as to the set up. I have a module and am using $route to successfully navigate but I'm lost as to what to do with my nav. When I load the module I want to read my database for a list of links that the user is allowed to access then spit them out in the nav. I don't want to repeat this in every view because I don't need to. So I'm trying to figure out how to run the ajax call once and then keep changing the view (I'd also like to add a class .selected to whatever view they're on). How would I go about doing that, with a directive?
(function () {
var app = angular.module('manage', ['ngRoute', 'manageControllers']);
/*
I've tried this but obviously $http isn't injected. Can I even do that?
var thisApp = this;
$http.get('/test/angular/php/angular.php', {params: {'function': 'nav'}}).then(function successCallback(response) {
});
*/
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'templates/dash.html',
controller: 'DashCtrl'
}).
when('/inventory/', {
templateUrl: 'templates/inventory.html',
controller: 'InventoryCtrl'
}).
when('/inventory/:mvKey', {
templateUrl: 'templates/inventory.html',
controller: 'InventoryCtrl'
}).
when('/inventory/:mvKey/:tab', {
templateUrl: 'templates/inventory.html',
controller: 'InventoryCtrl'
}).
/* etc...*/
}
]);
})();
EDIT:
My attempt at getting the nav to run once
controllers.js
var manageControllers = angular.module('manageControllers', []);
var thisApp = this;
nav = null;
navSelected = '/';
manageControllers.controller('NavCtrl', ['$scope', '$http', function($scope, $http) {
if (thisApp.nav === null) {
$http.get('php/angular.php', {params: {'function': 'nav'}}).then(function successCallback(response) {
console.log(response.data);
thisApp.nav = response.data;
$scope.nav = thisApp.nav;
$scope.select = thisApp.navSelected;
});
} else {
$scope.nav = thisApp.nav;
$scope.select = thisApp.navSelected;
}
}]);
manageControllers.controller('DashCtrl', ['$scope', function($scope) {
thisApp.navSelected = '/';
}]);
I would swith to UI Router (https://github.com/angular-ui/ui-router) instead of $route. It allows you being much more flexible with your routing.
A Small example:
app.config(['$stateProvider',
function($stateProvider) {
$stateProvider.
state('/', {
url: '/',
views: {
'': {
templateUrl: 'templates/dash.html',
controller: 'DashCtrl'
},
'nav#': {
templateUrl: 'path/to/nav.html',
controller: 'NavCtrl'
},
}
}).
state('/inventory/', {
url: '/',
views: {
'': {
templateUrl: 'templates/dash.html',
controller: 'DashCtrl'
},
'nav#': {
templateUrl: 'path/to/nav.html',
controller: 'NavCtrl'
},
}
}).
// ...
and in your index.html
<div ui-view="nav"></div>
<div ui-view ></div>
Take a closer look at UI Router's doc, there's much more you can do with it!

ocLazyLoad + UI Router navigating back to child view after page refresh

I'm using ocLazyLoad with AngularJS and AngularJS UI Router to load AngualarJS modules on demand. I have a Posts view/state and Post view/state in different modules and I can simply navigate between them.
The only issue is that when I'm on /posts/1 and manually refresh the page, it goes back to /. I understand that it makes perfect sense because app.js is the only file that gets loaded after page refresh but what I don't understand is how to make it lazy load the same /posts/1 view again after refreshing the page. I tried using $urlRouterProvider.when in app.js but had no luck.
I have created a plunker for this: http://plnkr.co/edit/lPb2qCGF9KLL16jVoLQo?p=info
app.js
angular.module('app', ['ui.router', 'oc.lazyLoad'])
.run(['$rootScope', '$state', '$stateParams',
function ($rootScope, $state, $stateParams) {
$rootScope.state = $state;
$rootScope.stateParams = $stateParams;
}
])
.config([
'$ocLazyLoadProvider',
'$stateProvider',
'$urlRouterProvider',
function ($ocLazyLoadProvider, $stateProvider, $urlRouterProvider) {
// For any unmatched url, redirect to /
$urlRouterProvider.otherwise('/');
$stateProvider
.state('app', {
url: '/',
views: {
header: {
template:
'<nav class="navbar navbar-default">' +
'<div class="container-fluid">' +
'<ul class="nav navbar-nav">' +
'<li><a ui-sref="app">Home</a></li>' +
'<li><a ui-sref="app.posts">Posts</a></li>' +
'</ul>' +
'</div>' +
'</nav>'
},
content: {
template: '<h3>Home view</h3>'
}
}
})
.state('app.posts', {
url: 'posts',
views: {
'content#': {
templateUrl: 'posts.html',
controller: 'PostsController',
resolve: {
load: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load({
serie: true,
files: [
'posts.js',
'posts-controller.js'
]
});
}]
}
}
}
});
}
]);
posts.js
angular.module('app.posts', ['ui.router', 'oc.lazyLoad'])
.config([
'$ocLazyLoadProvider',
'$stateProvider',
function ($ocLazyLoadProvider, $stateProvider) {
$stateProvider
.state('app.posts.post', {
url: '/:id',
views: {
post: {
templateUrl: 'post.html',
controller: 'PostController',
resolve: {
post: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load({
serie: true,
files: [
'post.js',
'post-controller.js'
]
});
}]
}
}
}
});
}
]);
post.js
angular.module('app.posts.post', []);
Lazy Loading ui-router states with ocLazyLoad and ui-router-extras futureStates
http://bardo.io/2014/08/26/oclazyload-future-states/

Resources