AngularJS, HTML5 mode routing not working - javascript

I am trying to rid my site of Hashbangs but I can't seem to get it quite right. For example, if I land on my home page and then click on the "CSS" menu and select the "Origami Element" menu option, I briefly see the page load before I am directed to a GitHub 404 page.
If I put the url (http://eat-sleep-code.com/css/origami) in directly, I get sent directly to the GitHub 404.
What am I missing, or is this not possible on a GitHub Pages-hosted AngularJS site?
Below is a partial chunck of my app.js
var app = angular.module('eatsleepcode', ['ngRoute', 'ngSanitize']);
/* Routing */
app.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {templateUrl: 'views/blog.html', controller: 'BlogController'}).
when('/blog', {templateUrl: 'views/blog.html', controller: 'BlogController'}).
when('/blog/:postID', {templateUrl: 'views/blog.html', controller: 'BlogController'}).
when('/contact', {templateUrl: 'views/contact.html', controller: 'DefaultController'}).
when('/privacy', {templateUrl: 'views/privacy.html', controller: 'DefaultController'}).
when('/resources', {templateUrl: 'views/resources.html', controller: 'DefaultController'}).
when('/terms', {templateUrl: 'views/terms.html', controller: 'DefaultController'}).
when('/css/origami', {templateUrl: 'views/css/origami.html', controller: 'DefaultController'}).
otherwise({
redirectTo: '/404'
});
$locationProvider.html5Mode(true);
}]);
/* Controllers */
app.controller('DefaultController', function($scope) {});

#zeroflagL suggestion got me over my first hurdle. I had some code that was specifically firing on hashbang URLs. I had failed to update that IF condition.
Now, clicking on all the links worked fine but entering a URL directly resulted in a 404 error. Had I been running this site on IIS or Apache I could have rectified the solution by implementing a URL rewrite (and this would be the ideal way to deal with this).
But alas, I am running this on GitHub pages. They currently (as of 11/25/2014) do not support setting up your own URL rewrite configuration.
They do however, let you setup a custom 404 page. I setup a simple 404.html page in the root of my GitHub pages site. This 404 page, inserts the hash AngularJS needs behind the scenes into the URL and then calls a redirect to it. As we are using a window.location.replace, the 404.html page doesn't show up in the browser history.
<html lang="en" data-ng-app="eatsleepcode">
<head>
<title><eat-sleep-code /></title>
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript">
var url = window.location.protocol + '//' + window.location.host + '/#' + window.location.pathname;
window.location.replace(url);
</script>
</body>
</html>
In the event the page doesn't really exist... ie: http://eat-sleep-code.com/somecrazyurl Angular routing takes over and loads my 404 view. Perhaps not the most elegant solution but it appears to have worked in a situation where URL rewriting is not an option.

The problem is in your render.min.js script. When you click on a link then the URL of the current window is changed:
window.top.location.href=e
Without that it works fine.

Related

Angular 1 Base href and routing/views/controllers

I am looking to add a base href to my webapp. Currently everything runs correctly when running:
localhost:3000/#/login
But i need to add base href or "charge" -> localhost:3000/charge/#/login
Current index.html
<head>
<base href="charge/" />
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" ....
</head>
Anuglar Route File
function routerConfig($routeProvider, $locationProvider) {
$routeProvider
.when('/map', {
templateUrl: 'app/views/main/main.html',
controller: 'MainController',
controllerAs: 'mainCtrl'
})
.when('/login', {
templateUrl: 'app/views/login/login.html',
controller: 'LoginController',
controllerAs: 'loginCtrl'
})
.when('/list', {
templateUrl: 'app/views/list/list.html',
controller: 'ListController',
controllerAs: 'listCtrl'
})
.when('/reporting', {
templateUrl: 'app/views/reporting/reporting.html',
controller: 'ReportingController',
controllerAs: 'reportingCtrl'
})
.otherwise({
redirectTo: '/map'
});
// $locationProvider.html5Mode(true);
}
Whenever i run my app with the basehref and navigate to localhost:3000/charge/#/login, basically every view, controller, bower component, and the module cannot be found. I have tried editing the router, changing the name of the main app folder but cannot get this to work. I know i am missing something small so any help is greatly appreciated!
My issue seems very similar to Base Href and Angular Routing and Views but i honestly just cannot figure this out.
If you're using Chrome and you look in the developer console, look at what the URL for the resources it can't find:
http://localhost:8080/charge/app.js
It is looking at the root of the project, and then for a folder called charge, and then for the resource.
The HTML element specifies the base URL to use for all relative
URLs contained within a document. There can be only one element
in a document. - MDN
Can you tell us what your directory structure looks like here? If your project files are directly in root, try making a subfolder named "charge" and put all of your files in it.
Example: root > charge > project files
Helpful link: AngularJS: Changing App Path, and the Element
Try prepending a "/" before all of your source URLs. This will begin each path at the root directory of your project rather than at your base href.
e.g. templateUrl: '/app/views/main/main.html'

ngRoute not finding template

I'm new to angular and am having a hard time getting ngRoute to pick up my template file.
Here is my index.html:
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="app.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/main.css" />
</head>
<nav>
<div>
<ul>
<li>home</li>
</ul>
</div>
</nav>
<body>
<div ng-view>
</div>
</body>
</html>
Here is my app.js:
var myApp = angular.module('myApp', ['ngRoute']);
myApp.controller('MainCtrl', function($scope) {
$scope.message = 'Hello World';
});
myApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'pages/home.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/home'
});
}]);
Here is my pages/home.html:
<div ng-controller="MainCtrl">
<p>Test</p>
<p>{{ message }}</p>
</div>
I can see that it is appending #/ to the root url so perhaps this is partially working; however, it doesn't seem to be rendering the template at "pages/home.html".
I've checked the cdn url's to make sure there wasn't any version inconsistencies, and what not, but that doesn't seem the be the case.
This is pretty much my first Angular project, and I've just been going off of the docs, but there must be something I'm not seeing. Coming from other server side projects, the lack of stack trace is killing me haha.
Is there something I'm missing in the above code, that is preventing my template from being rendered in '/'?
Thanks!
This is my code and it is working fine, you cam take help from it.
var EventList = angular.module("EventList", ['ngRoute' ,'infinite-scroll']);
EventList.config(function($routeProvider) {
//$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
templateUrl: 'views/business/business_home_events.html',
controller: 'EventListController'
});
});
EventList.controller('EventListController', ['$scope', '$http', '$route', function($scope, $http, $route){
// Do your work
}]);
the problem is with your when ('/'). Since your url has #/home - it looks in the .when to find that route.
change it to
myApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/home', { // <-
templateUrl: 'pages/home.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
}]);
This was an annoying one, but I think I figured it out.
I was encountering this issue while testing locally (no nodejs) on Chrome; however, sure enough when I popped it open in Safari it was working. It seems that ngRoute chokes on local files in Chrome.
I found a reference to the issue here, which is closed, so I'm guessing if I update to a newer version I shouldn't have this issue.
https://github.com/angular/angular.js/issues/4680
Setting up a web server should resolve this problem.
Why AngularJS routes are not working in local?
I have executed you code and is working fine on mozilla.
However, there is an issue in chrome of cross origin request if we run the file without putting in server.
But it is working fine on chrome also if you will put it on server (may be xampp/wampp) and run the file. The angular library you are using have http request to another server.

AngularJS routing to static file

I have a simple angularjs app, with ngRoute module for routing in html5Mode.
How can I have a link to some static file on my page, and not to have it intercepted by angular routing module?
Here's the example:
HTML:
<head>
<base href='/'></base>
</head>
<body ng-app="crudApp">
Home
User
users.html
<div ng-view></div>
JS routing:
$routeProvider
.when('/', {
templateUrl: 'app/components/home/homeView.html',
controller: 'HomeController'
})
.when('/user', {
templateUrl: 'app/components/user/userView.html',
controller: 'UserController'
})
.otherwise({
redirectTo: '/'
});
When I click on User link I get routed to localhost:8080/user, and my controller and template work fine. When I click on users.html link I get routed to home, but I want to invoke a static home.html page.
From the AngularJS docs, you have 3 options:
Html link rewriting
(...)
In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.
Links that contain target element
Example: link
Absolute links that go to a different domain
Example: link
Links starting with '/' that lead to a different base path
Example: link
What you might be looking for is the first example:
users.html

AngularJS loading different views through routes

I want to display two pages, but use a base layout. I have it somewhat working with the following:
index.html
<html data-ng-app="myApp">
<head>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
</head>
<body>
<div data-ng-view class="container"></div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="public/app.js"></script>
</body>
</html>
main.html
<div>
<h2> Hello! This is Main page </h2>
</div>
list.html
<div>
<h2> This is List page </h2>
</div>
app.js
var myApp = angular.module('myApp', []);
// Routing Setup
function myAppRouteConfig($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'index.html'
}).
when('/list', {
controller: ListController,
templateUrl: 'list.html'
}).
otherwise({
redirectTo: '/'
});
}
myApp.config(myAppRouteConfig);
This somewhat works when I visit index.html and list.html, but two problems:
When I load index.html, bootstrap loads fine. But when I visit list.html, bootstrap doesn't load. In fact, looking at the html source in firebug, all the code from index.html isn't loaded. The container is missing, the script and css links are missing.
How do I load an actual index page? I have my main.html that I want to load when a user visits the root page, but index.html is the base layout that contains code that persists through all other views (ie, like header and footer etc). If I modify my app.js and set the templateUrl: 'main.html', it seems to still load index.html. Is AngularJS implicitly looking for index.html as the base template?
EDIT:
File structure:
-- server.js
-- public/
|-- index.html
|-- list.html
|-- main.html
|-- js
|-- app.js
Change your route to:
$routeProvider.
when('/', {
templateUrl: 'main.html'
}).
when('/list', {
controller: ListController,
templateUrl: 'list.html'
}).
//if you need to use login page, add 1 more route
when('/login', {
templateUrl: 'login.html'
})
otherwise({
redirectTo: '/'
});
and put your index.html at the root directory (or any sub directory) of your web app, configure it as the default document.
Is AngularJS implicitly looking for index.html as the base template?
There is nothing related to angular here, this is the normal behavior of loading an html page from a web server.
Here is how it works:
When users access your application at the root url (e.x: http://example.com) or any sub directory (http://example.com/public), the index.html is loaded into browser like with normal web applications, then your app.js is run as normal. When the routes are registered and the application is bootstrapped, angular checks the route and loads main.html to be inserted into the container where ng-view is declared.
After digging around, it turns out my AngularJS route requires ngRoute, which is its own module now. After including it, it started to display the correct pages.

ASP MVC url with angularjs

In asp web api i have an index.html with angularjs framework.
In angularjs i have the following route:
gestionale.config(function ($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: 'View/people.html',
controller: 'mainController'
}).
when('/ruoli', {
templateUrl: 'View/ruoli.html',
controller: 'ruoliController'
});
});
When i start the project with visual studio, it opens the index.html at the following url:
http://localhost:49375/index.html#/
and the view "View/people.html" is correctly showed.
1)How can i put, in the index.html, a static link to the ruoli.html page? I have tried
<a href="/ruoli">
but doesn't work because it load the page
http://localhost:49375/ruoli
instead of
http://localhost:49375/index.html#/ruoli
You need to put the hashbang before the slash.
So you setup your href attribute like this :
<a href="#/ruoli">
that will properly navigate to:
http://localhost:49375/index.html#/ruoli
Check this hashbang routing article/cookbook:
http://fdietz.github.io/recipes-with-angular-js/urls-routing-and-partials/client-side-routing-with-hashbang-urls.html

Categories

Resources