Page refresh redirects to home page - angularjs

I have created a form in angularjs. I has four pages like information, education, experience and privacy. while filling the details in education if the page is refreshed it redirects to information that is starting page of the form. If the page is refreshed I want to stay in current page. Give me any suggestion. My routing code given below
(function() {
"use strict";
xyz.config(['$stateProvider',
function ($stateProvider) {
'use strict';
$stateProvider
.state('xyz.basicInfo', {
url: '/Information',
views: {
'Form': {
title: "Information", 
templateUrl: '../views/information.html',
controller: 'InformationController'
}
},
resolve: {
loadFile: ['$ocLazyLoad', function($ocLazyLoad) {
return $ocLazyLoad.load(['intlTelInput']);
}]
}
})
.state('xyz.education', {
url: '/education',
views: {
'Form': {
title: "Education",
templateUrl: '../views/education.html',
controller: 'educationController'
}
}
})
}])
}).call(this);

This kind of issue is typically a web server issue. Angular apps are Single Page Apps (SPAs) so any request for an HTML file, regardless of path, should serve up index.html (index.html being the single page)
If for example, your route is /my/route and you refresh the web server, by default, will try to serve up index.html inside a folder called /my/route
So you need to override this default behaviour such that your webserver always servers the root index.html and then your routes will work as expected.
How to do this depends on your web server - if its a node server, apache, nginx, webpack-dev-server, etc
Heres a thread talking about how to do this on Apache ... https://askubuntu.com/questions/484986/how-do-i-make-apache-serve-a-single-static-page-no-matter-what-the-entered-url-i

Related

ui routing not work properly on server

when we click on module section then ui-router URL change but template not load at the current time. after few minutes after template load.
ui-router work properly on localhost but when we deploy on ec2 server then face this problem.
We have state is:
.state("Modules", {
url: "/admin/Modules",
templateUrl: "angular/templates/modules.html",
controller: "ModuleController",
resolve: {
$title: function () {
return 'Modules';
}
}
})

Angular Routing with ASP MVC

I have an MVC test project I am working on to see if I can get angular routing to work with it.
What I mean by work with it is that I want to be able to have a landing page for my app: www.testapp.com
Then when people log in I want MVC to route them to testapp.com/Dashboard/#/and then I want to be able to use ng-view to load pages with Angualar like so:
testapp.com/Dashboard/#/PageOne, testapp.com/Dashboard/#/PageTwo, etc.
Here is what I have tried:
main.js:
angular.module('App', ['ngRoute', 'ngResource']);
angular.module('App').config(function ($routeProvider) {
$routeProvider
.when('/Dashboard', {
templateUrl: 'Dashboard/Index'
})
.when('/PageOne', {
templateUrl: 'Dashboard/PageOne'
})
});
~/Views/Home/Index.cshtml was my landing page that did not use angular routing it just had ActionLinks to Login and Register
~/Views/Dashboard/Index.cshtml is where I used ng-app and ng-view and I had links like so: Dashboard, Page One
The problem is that when I go to testapp.com/Dashboard when it loads the URL turns into testapp.com/Dashboard#/ rather than testapp.com/Dashboard/#/
The other problem is that when I click on my links it goes straight back to the Home/Index and the URL is like so: testapp.com/#/PageOne but the Home is being displayed
in my RouteConfig.cs file it is just the default:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
So my question is what is wrong with my code here that makes it not function like I want it to? What do I have to add/change?
Thank you!
So I figured out the issue was with the way I was writing my links. Once my actual angular app was loaded (I was at the Dashboard rather than the landing page where angular is initialized) I had to change my anchor tag hrefs from \#\{Route} to '/Dashboard/#/{Route}'. Here is my updated angular route config:
angular.module('App').config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'Index'
})
.when('/PageOne', {
templateUrl: 'PageOne'
})
});
This completely fixed my problem so I can now have angular routing take over the show when it needs to and also have MVC ActionLinks in my application as well.
Hope this helps someone else!
NOTE
I did not change anything in my MVC RouteConfig.cs you can leave the default. Also I had to get rid of ViewStart because that was messing things up.

AngularJS ui.router root state should be called always during app init

I am working on web application using AngularJS and have used ui.router for routing.
I have configured the app
.state('init', {
url: '/',
controller: 'LocalizationCtrl',
templateUrl: 'partials/common/init.html'
})
.state('login', {
url: '/login',
templateUrl: 'partials/auth/login.html',
controller: 'LoginCtrl',
resolve: {
skipIfLoggedIn: skipIfLoggedIn
}
});
In init I load the localization json from server
If I hit the following URL it all works fine
http://localhost/app/index.html
However if I hit the following URL or any other state directly the localization files do not load
http://localhost/app/index.html#/login
How can I make sure that when app is loaded first using any URL the localization code should execute and not bypassed.
/ Bu default go to state -
angular.module('yourModuleName').run(["$location", function ($location) {
$location.url('/');
}]);
So, when you refresh or take your web application, your site will go to url /, so it should invoke your state, init to work.
Or you can use $state to go to a state upon starts
/ Bu default go to state -
angular.module('yourModuleName').run(["$state", function ($state) {
$state.go('init');
}]);
Its very simple there is a property "Resolve" you can use that.
In your parent state you can write this -
.state('parentState', {
resolve : {
localize : function() {
//Localization code here
}
}
};
Resolve will ensure that your localization work will be done before controller is loaded.

How to manage the output page in Angular JS - routeProvider?

My project was built on PHP and has some pages on Angular JS. There are three types of accounts in system. Authorization system is based on PHP/Mysql.
For each account I have personal.html template. When user is logged via account(1) I need to show him template /public/html/personal.html from route, if account(2) - template /public/html/personal2.html .etc. So, path /profile/personal/:type is the same for each.
How I can manage this accounts pages with a good level of security?
I use routeProvider:
$routeProvider
.when('/profile/personal/:type', {
templateUrl: '/public/html/personal.html',
controller: 'ProfileController'
})
You can use a function as templateUrl parameter and use it to select the template for the current account type:
$routeProvider
.when('/profile/personal/:type', {
templateUrl: function(params) {
switch(params.type) {
case 1:
return: '/public/html/personal1.html';
}
},
controller: 'ProfileController'
});

How can I get AngularJS ui-router to respond correctly to a browser page refresh?

I have created an AngularJS application that uses ur-router. Here's a small sample of the config:
$locationProvider.html5Mode(true);
var admin = {
name: 'admin',
url: '/Admin',
views: {
'root': {
templateUrl: '/app/admin/partials/home.html',
},
'content': {
templateUrl: '/app/admin/partials/overview.html',
}
}
};
var adminContent = {
name: 'admin.content',
parent: 'admin',
url: '/:content',
views: {
'root': {
templateUrl: '/app/admin/partials/home.html',
},
'content': {
templateUrl: function (stateParams) {
return '/app/admin/partials/' + stateParams.content + '.html';
},
}
}
};
Everything is working when I start up the application, then go to the /Admin with a link and next I go to the reference link and it brings up the page:
http://xx.com/Admin/reference
When I click the browser back button it goes back to the previous page as expected.
However if I do a refresh now everything goes wrong. It forgets about ui-router and tries to find the page: xx.com/Admin/Reference
For reference I am using: #version v0.2.13
My index file looks like this:
<head>
<base href="/" />
Can someone give me some suggestions as to what I am doing wrong?
This is happening because you are using HTML5mode. You can either disable it or configure your server to handle it.
When you click a link on the page, the javascript changes the displayed url, but it does not actually navigate to the new url. When you refresh, the browser makes a request to the server for the new url. The server is treating it as a regular request, but it does not have any page at that url, so it returns a 404. You can configure the server to redirect any url to your main page so that this doesn't happen. When you refresh (with proper configuration), the server will send back index.html (or whatever your main page is) and the javascript can fetch the arguments out of the url.
This doc explains how to configure the server in ui-router. https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode
You will also need a <base> tag so that angular can tell the difference between the url arguments and the rest of the url.
Example <base> tag:
If your site is http://www.example.com/ and you are on the Admin page (http://www.example.com/Admin), your base tag would look like <base href="http://www.example.com/">
For more info on the different routing modes in Angular see: https://docs.angularjs.org/guide/$location

Resources