stateprovider is not working in switching pages - angularjs

In my program i want to load programsetup html page after pressing sign in button in login page.
myconfig function for login pages as follows
.config(function config($stateProvider) {
$stateProvider.state('login', {
url: '/login',
views: {
"main": {
controller: 'LoginCtrl',
templateUrl: 'login/login.tpl.html'
}
},
data: {
pageTitle: 'Login'
}
});
})
login page loads successfully! but program setup page is not loading after click on sign in button
$location.href = "#/programsetup";
this is line i used in my login .js for loading program setup page my programsetup config function is:
.config(function config($stateProvider) {
$stateProvider.state('programsetup', {
url: '/programsetup',
views: {
"main": {
controller: 'ProgramSetupCtrl',
templateUrl: 'programsetup/programsetup.tpl.html'
}
},
data: {
pageTitle: 'Program Setup'
}
});
})
what mistake i am doing? please help me out.

In your login.js file(inside login controller) you need to write ng-click method and add below code
$state.go("programsetup");
And $state need to add dependency injection in your controller

Just configure your state for programsetup inside the root .config. Then for the button login. User $state.go('programsetup') to go to that page.
Rough Html:
<button class="button button-positive" ng-click="login()">Go Programsetup</button>
Then in the config and controller.
.config(function config($stateProvider) {
$stateProvider.state('login', {
url: '/login',
views: {
"main": {
controller: 'LoginCtrl',
templateUrl: 'login/login.tpl.html'
}
},
data: {
pageTitle: 'Login'
}
});
$stateProvider.state('programsetup', {
url: '/programsetup',
views: {
"main": {
controller: 'ProgramSetupCtrl',
templateUrl: 'programsetup/programsetup.tpl.html'
}
},
data: {
pageTitle: 'Program Setup'
}
});
})
.controller('LoginCtrl', function($scope, $state) {
$scope.login = function() {
$state.go('programsetup');
}
})
.controller('ProgramSetupCtrl', function($scope) {
alert('i am at Program setup page.');
})

Related

Ionic 1 - Show login page if user not logged and skip if user is already logged

Working on an Ionic version 1.3.3 application where need following functionalities for user login. I had go through all stackoverflow answer but nothing found a workable solution for me.
App will check on start if user already logged in (check through Ionic $localstorage) then redirect to Home page
If the user is not logged redirect to login page on app start
On login page after login success redirect to home page and clear login page history.
angular.module('starter', ['ionic', 'starter.controllers', 'starter.directives', 'starter.services', 'ngStorage','ab-base64',])
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
cache: false,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl',
onEnter: function ($state) {
console.log($state);
}
})
.state('app.home', {
cache: false,
url: '/home',
views: {
'menuContent': {
templateUrl: 'templates/home.html'
}
}
})
.state('app.login', {
cache: false,
url: '/login/:username/:password',
views: {
'menuContent': {
templateUrl: 'templates/login.html',
controller: 'LoginController'
}
}
})
.state('app.profile', {
cache: false,
url: '/profile',
views: {
'menuContent': {
templateUrl: 'templates/profile.html',
controller: 'ProfileController'
}
}
})
$urlRouterProvider.otherwise('/app/home');
})
This is how I accomplished this in Ionic v1:
For the redirect if user is logged in:
.state("app.dash", {
url: "/dashboard",
abstract: true,
views: {
mainContent: {
templateUrl: "templates/dashboard.html",
controller: "DashboardCtrl",
controllerAs: "vm",
resolve: {
auth: [
"authService",
function(authService) {
return authService.isAuthenticated();
}
],
permissions: [
"authService",
function(authService) {
return authService.getPermissions();
}
]
}
}
}
})
For the redirect when user logs in or is already logged in.
.state("app.login", {
url: "/login?accountCreated",
views: {
mainContent: {
templateUrl: "templates/login.html",
controller: "LoginCtrl",
controllerAs: "vm",
resolve: {
isLoggedIn: [
"$q",
"$state",
"authService",
function($q, $state, authService) {
authService.isAuthenticated().then(function(res) {
$state.go("app.dash.home");
});
return $q.defer().resolve();
}
]
}
}
}
})
Auth service isAuthenticated()
function isAuthenticated() {
var deferred = $q.defer();
getToken().then(function(token) {
isExpired().then(function(isExpired) {
if (!token || isExpired) {
deferred.reject("Not Authenticated");
} else {
decodeToken().then(function(decodedToken) {
deferred.resolve(decodedToken);
});
}
});
});
return deferred.promise;
}

Opening link in new browser redirecting me to login page in Angular JS

I have AngularJS website which contains few hyperlink. Whenever I try to open the link in same tab then it is working fine but whenever I try to open the link in new tab then it is redirecting me to login page.
RouteController.js
var app = angular.module("appHome", ['ui.router']);
app.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/login');
$stateProvider
.state('introduction', {
url: '/',
views: {
'mainview':
{
templateUrl: 'HomeLoginPages/Login.html',
controller: 'ctrlLogin'
}
}
})
.state('login', {
url: '/login',
views: {
'mainview':
{
templateUrl: 'HomeLoginPages/Login.html',
controller: 'ctrlLogin'
}
}
})
.state('home.eventlist.requestdetail', {
url: '/requestdetail',
params: { paramOne: "defaultValueOne", paramTwo: "defaultValueTwo", paramThree: "defaultValueThree", paramFour: "defaultValueFour" },
views: {
'homedetailview#home':
{
templateUrl: 'RequestPages/DetailedRequest.html',
controller: 'ctrlRequestDetail',
}
}
})
})
HTML File
<form class="form-main">
<div class="div-main">
<div class="gridBigStyle" ui-grid="gridEventList" ui-grid-pagination>
</div>
</div>
</form>
Controller.js
var myApp = angular.module('appHome');
myApp.controller("ctrlEventList", ['$scope', 'MetadataOrgFactory', function ($scope, MetadataOrgFactory) {
$scope.gridEventList = {
data: 'eventlistdata',
columnDefs: [
{ field: 'SiteEventNumber', displayName: 'Event Number' },
{
field: 'RequestNumber', displayName: 'Event Request',
cellTemplate: '<a ui-sref=".requestdetail({paramOne:row.entity.RequestId, paramTwo:row.entity.EventId, paramThree: 1,paramFour: defaultValueFour})" class="ui-grid-cell-contents">{{COL_FIELD}}</a>'
},
]
}
}]);
I am using Token Authentication during login of user. The token is valid always but it is still redirecting me to Login Page.
I have saved the login information in localstorage variable which is used in all tabs. When user log out then I have explicitly set the value to blank so that all other tabs page will be redirect to login page. I hope this answer will help others.

$state.go is not working when project is run with ionic serve --lab command

Hello I am working with ionic framework and I want to redirect to another page
after successfully login .When I run project using command:
ionic serve then $state.go working properly but when I run project using
ionic serve --lab it is not working
.controller('AppCtrl', function($scope,$http, $ionicModal,$location,$state, $timeout) {
// Form data for the login modal
$scope.loginData = {};
// Create the login modal that we will use later
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
// Triggered in the login modal to close it
$scope.closeLogin = function() {
$scope.modal.hide();
};
// Open the login modal
$scope.login = function() {
$scope.modal.show();
};
// Perform the login action when the user submits the login form
$scope.doLogin = function() {
alert($scope.loginData.username);
alert($scope.loginData.password);
$http({
method: 'POST',
url: 'http://localhost/home_owner12/admin/api/login',
data: {username:$scope.loginData.username,password:$scope.loginData.password},
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'}
})
.then(function successCallback(response)
{
if(response.data.length > 0)
{
$state.go('app.search');
console.log('the state is '+$state.current);
}
else
{
alert("Invalid email or pasword");
///$location.path('/search');
}
}, function errorCallback(response)
{
alert("Invalid email pasword");
});
};
})
here is state:
config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('app.search', {
url: '/search',
views: {
'menuContent': {
templateUrl: 'templates/search.html',
controller: 'AppCtrl'
}
}
});
and module
angular.module('starter', ['ionic', 'starter.controllers', 'ui.router'])
The app.search page's controller goes to AppCtrl in place.
Because AppCtrl runs a second time, it is entering an infinite loop.
Replace the code with that
config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('app.search', {
url: '/search',
views: {
'menuContent': {
templateUrl: 'templates/search.html',
controller: 'SearchCtrl'
}
}
});
and
.controller('SearchCtrl', function($scope,$http, $ionicModal,$location,$state, $timeout) {
............
.........
.......
})

Cannot navigate by typing the state's name in browser's url

mainApp.config(['$stateProvider', '$routeProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider',
function($stateProvider, $routeProvider, $urlRouterProvider, $httpProvider, $locationProvider) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
$stateProvider
.state('page', {
abstract: true,
url: '/newgmr/admin',
views: {
'home': {
template: '<div ui-view="body"></div>'
}
}
}).state('page.login', {
url: '/',
views: {
'body#page': {
templateUrl: 'mainApp/landingPage/login.html',
controller: 'loginController'
}
}
})
.state('page.forgotpassword', {
url: '/forgot-password',
views: {
'body#page': {
templateUrl: 'mainApp/landingPage/forgotPassword.html',
controller: 'forgotPasswordController'
}
}
})
.state('page.resetpassword', {
url: '/reset',
views: {
'body#page': {
templateUrl: 'mainApp/landingPage/resetPage.html',
controller: 'resetPasswordController'
}
}
})
.state('admin.dashboard', {
url: '/dashboard',
views: {
'header#admin': {
templateUrl: 'mainApp/dashboardPage/header.html'
},
'leftPane#admin': {
templateUrl: 'mainApp/dashboardPage/leftPane.html',
controller : 'leftSidePaneController'
},
'body#admin': {
templateUrl: 'mainApp/dashboardPage/tabHolder.html'
},
'footer#admin': {
templateUrl: 'mainApp/dashboardPage/footer.html'
}
}
});
$urlRouterProvider.
otherwise('/');
I am newbie to angular. When I give, localhost/newgmr/admin it loads my app. But when I try to navigate to any other state, say forgot-password screen by typing localhost/newgmr/admin/forgot-password into the browser's url and hit enter, the app shows 404 page. However, I can achieve this by clicking on a link which will call the state "page.forgotpassword". I understand that this happens because there is no actual directory like forgot-password and there is no index.html in that location either. So, please help me how to achieve this. User must be able to navigate to a state by typing the url as well.
If the data provided is not enough, please let me know.

state parameter value is mixed with url in UI-Router in angularjs when refresh is clicked

I am trying to creating web page to perform on edit actions in both in parent(first.edit) and child(second.edit) states.
I sending related state parameters through URL, those are captured by using regex patterns.
When I click on ui-sref="home.first.edit({firstId:10})" link the controller receiving state parameter firstId correctly i.e 10.
When I click on ui-sref="home.first.second.edit({firstId:10,secondId:20})" link or refresh the the state the controller receiving in correct value for firstId i.e 10/second/20.
and it is moving to parent state(home.first)
This is my js file
angular.module('MyApp', [
'ui.router'
])
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
Here are the states:
$stateProvider.state('home', {
url: '',
views: {
'home_view': {
templateUrl: 'index.html',
controller: 'homeController'
}
}
})
.state('home.first', {
url: "/first",
params:{
firstId:null
},
views: {
'home_view': {
templateUrl: "first.html",
controller: 'firstCtrl'
}
}
})
.state('home.first.edit', {
url: "/{firstId:[0-9]+}/edit",
views: {
'first_view': {
templateUrl: "/edit_first.html",
controller: 'firstCtrl'
}
}
})
.state('home.first.second', {
url: "/second",
params:{
secondId:null
}
views: {
'first_view': {
templateUrl: "/second.html",
controller: 'secondCtrl'
}
}
})
.state('home.first.second.edit', {
url: "/{secondId:[0-9]+}/edit",
views: {
'second_view': {
templateUrl: "/views/testplan/projects/edit_second.html",
controller: 'secondCtrl'
}
}
})
Here is the rest of the code with controller definition
.controller(
'homeController',
function($scope, $state, $stateParams) {
// console.log($stateParams.firstId);
}
)
.controller(
'firstCtrl',
function($scope, $state, $stateParams) {
console.log($stateParams.firstId);
}
)
.controller(
'secondCtrl',
function($scope, $state, $stateParams) {
console.log($stateParams.secondId);
}
);
There is a working plunker
Your state definition could be simplified like this:
.state('home.first', {
url: "/first",
...
.state('home.first.edit', {
url: "/{firstId:[0-9]+}/edit",
...
.state('home.first.second', {
url: "/second",
...
.state('home.first.second.edit', {
url: "/{secondId:[0-9]+}/edit",
But the way you are expecting it to work should be like this:
.state('home.first', {
url: "/first/{firstId:[0-9]+}",
...
.state('home.first.edit', {
url: "/edit",
..
.state('home.first.second', {
url: "/second/{secondId:[0-9]+}",
...
.state('home.first.second.edit', {
url: "/edit",
These links will start to work then:
<a ui-sref="home.first({firstId:1})">
<a ui-sref="home.first({firstId:22})">
<a ui-sref="home.first.edit({firstId:333})">
<a ui-sref="home.first.edit({firstId:44444})">
<a ui-sref="home.first.second({firstId:4444})">
<a ui-sref="home.first.second.edit({firstId:1,secondId:333})">
<a ui-sref="home.first.second.edit({firstId:22,secondId:55555})">
Check it in action here

Resources