I'm new to angular. I create a blog for showing my profile.
In my website, I have several component and my app.module has a configuration for the routing, as shown:
app.config(function($locationProvider, $routeProvider){
$locationProvider.html5Mode({
enabled:true,
requireBase: false
})
$routeProvider.
when("/", {
template: "<research-overview></research-overview>"
}).
when("/research-interests", {
template: "<research-interests></research-interests>"
}).
otherwise({
redirectTo: "/"
})
});
Here is an example of my component:
angular.module('researchOverview', [])
.component('researchOverview', {
templateUrl: '/templates/research-overview.html',
});
I tried to locally run the server using python -m SimpleHttpServer
so that I can access my local website using browser to localhost:8000, When I click a link from my homepage, the page is correctly loaded. However, when I initially access my website using different urls (e.g. localhost:8000/testing) Angular route doesn't seem to work properly.
I get this error instead,
How do I set the router so that those urls can be redirected to my homepage?
Given you have enabled html5 mode I think you may need to set your server up so that regardless of the path it will return your index.html file.
For example, when your server receives a request for a resource at localhost:8000/testing it will return index.html.
From the AngularJS documentation:
Using this mode requires URL rewriting on server side, basically you
have to rewrite all your links to entry point of your application
(e.g. index.html). Requiring a tag is also important for this
case, as it allows Angular to differentiate between the part of the
url that is the application base and the path that should be handled
by the application.
Related
I have an existing Django project and am adding a new app which consists of an API (using the rest-framework) and an angular app. This app contains one Django template, which simply serves as a way for Angular to hook in, and then work with it's own partials:
{% extends "base.html" %}
{% block content %}
<div ui-view></div>
{% endblock %}
From here, I would like for angular to take over the routing of the application:
$resourceProvider.defaults.stripTrailingSlashes = false;
// Routing
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
$stateProvider
.state('list', {
url: '/routines',
templateUrl: 'static/partials/routine/routine_list.html',
controller: 'RoutineListController',
})
.state('add', {
url: '/routines/add',
templateUrl: 'static/partials/routine/routine_add_edit.html',
controller: 'RoutineAddController',
});
$urlRouterProvider.otherwise('/routines');
This works fine for the first page the app loads, however attempting to navigate to any subsequent page results in Django displaying a 404 error message
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/routines/add
Based on my current (limited) knowledge of both systems, I attempted to specify a url in my Django config that would apply to all routine/* urls and not load a template, allowing angular to request the partial it requires so it can continue running the SPA uninterrupted. However, I was unable to find any information on specifying a URL without a template, leading me to believe that this is not the correct solution.
urlpatterns = [
url(r'^$', views.routineView, name='routine'),
url(r'^/.*$', views.routineView, name='angular'),
Summary:
I would like to use Angular and a Django rest API app to create a web application. However, I would like to keep the rest of my existing Django site and simply add this on (an embedded SPA in my current Django base.html template). How do I set up the routing (on both Angular and Django) so that Django allows Angular to run interrupted once a certain page has been requested (in this case site.com/routines).
As far as I understood, you simply want to use AngularJS with Django Rest API along with Django templates for some routes. I did this before using this https://github.com/mgonto/restangular. All you need to create a service that represent the resource. Here is a sample code for the service in AngularJS:
(function() {
function Course(Restangular) {
var service = Restangular.all("course");
//studentStats refers to the corresponding JS function that will call the API (note API URL is configured in app.js)
//student_stats refers to the corresponding url in Django API
service.addRestangularMethod("studentStats", "get", "student_statuses");
return service;
}
angular.module("YOUR-APP")
.service("Course", ["Restangular", Course]);
}).call(null);
I'm developing a single page application (SPA) using Sails.js as a backend. All I want is to redirect all routes to a single controller action.
However, when I do the following:
// config/routes.js
module.exports.routes = {
'GET *': 'MainController.application'
};
All requests are getting redirected to my application route, even for static files like CSS/JavaScript, etc. Is there an easy way to fallback to my application route when there is no other means to handle it?
I want:
All static files to be served directly (JS, CSS, HTML partials)
All specific routes to be handled as is
In other case redirect to a single entry-point controller
After thorough re-reading of the docs I've found a skipAssets route parameter.
Here's my new configuration:
// config/routes.js
module.exports.routes = {
'GET *': {
controller: 'MainController',
action: 'application',
skipAssets: true
}
};
Looks like it's working as required.
I have a homepage link that loads /register html page. But when I change css on the /register page and want to see it I have to go back to my localhost and then click the link again so the page loads again with new css. This is painfully time-consuming, is there a way to link /register with the page/route? Or at least remove /register from URL (so that localhost is only url for the whole app) so when the user refreshes the homepage welcomes him?
Homepage link:
REGISTER
View gets loaded like this
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
.when("/register", {
templateUrl: "/register",
controller: "registerController"
})
.otherwise("/");
$locationProvider.html5Mode(true);
}]);
This is an angular mechanic and is explained more in a recent question that I answered AngularJS + UI-Router - Manually Type URLs in HTML5 Mode without HashBang
Easy work around: use localhost/#/register instead to get to the page.
Since you are using
$location.html5mode(true);
This issue is directly related to how the files are being served to the browser. Your angular app itself only has one access point and that is your index.html page. When you type into your browser localhost/register, it is looking for the register directory, not the actual angular route. Since you've enabled html5 mode, it removed the hash bangs, which looks nice, but that requires additional configuration to be able to access the views individually without them.
Additional: If you want to remove the route URLs altogether, you will need to use stateProvider instead of routeProvider
Related article regarding stateProvider: Angular ui-router: Can you change state without changing URL?
Set in you html
<head>
<base href="/yor base url">
</head>
At the minute I'm currently using the $routeProvider to dynamically load sections of the page like so:
$routeProvider
.when('/', {
templateUrl: '/pages/home.html',
controller: 'mainController'
})
.when('/our-business', {
templateUrl: '/pages/our-business.html',
controller: 'businessController',
css: 'css/_business.css'
})
.when('/solutions', {
templateUrl: '/pages/solutions.html',
controller: 'solutionsController'
});
Currently, if I go directly to the index (localhost) and then select 'Our Business' from the navigation menu then Angular handles the location request and the page loads fine, with the URL changing to localhost/our-business. If I then reload, or open this URL directly I get a 404 error - presumably because Apache is trying to open our-business.html which doesn't exist. If I open localhost/#/our-business then the index is loaded and Angular then handles the request. The issue I've got is that this is designed to be a public facing website, so if a user were to copy and paste the URL or share it via email, they'll get a 404 error.
Is there any way to have Apache rewrite URLs to parse them via the index and AngularJS so that we can keep the non-hash style but still have functional URLs?
As said Kailash you can set the locationProvider to html5 mode
$locationProvider.html5Mode(true)
when you bootstrap your angular application.
Then you have to tell your Apache server to send the index.html (entry point of you single page app) for any requested url.
The angular router will then handle the proper route
Nope. Changing the url without the hash reloads the entire page.
The entire point of this $routeProvider is to build single-page app with multiple views.
Basically, having the # in the url is the only way to do that. Changes to a url's hash don't result in a page reload, and allow Angular to load the relevant views.
I am creating a web application with ASP.NET MVC and AngularJS.
When I debug the web application the URL is like this:
http://localhost/Home/Index#/Login
When I publish it to the IIS development server I use then I use the Default Web Site, so the url will be like this:
http://ipaddress/MyApp/Home/Index#/Login
Because of this my directives do not work.
They are set up like this:
'/Login':
{
templateUrl: '/Account/Login',
controller: 'AccountController'
},
On the IIS development server this directive calls the route of the Web application:
http://ipaddress/Account/Login
which should be
http://ipaddress/MyApp/Account/Login
Is there a way to change the directives to work in both cases?
I have tried to play with the "/" character in front of the templateUrl, but it does not seem to work (it gets localhost/Home/Account/Login instead of localhost/Account/Login)
Just ran across this looking for an answer to a similar problem related to hosting an Angular app in an IIS application below a site. I was able to fix the TemplateUrl problem by just adding a . before the /
'/Login':
{
templateUrl: './Account/Login',
controller: 'AccountController'
},
There are 2 options I can provide you with:
Server config
Don't use virtual directories to host your app in the dev
environment. Rather host different sites with different host headers
in IIS so that you can refer to stuff from the root, for example:
/Account/Login
Configuration module
Consider using a module which gets injected into your angular app
which contains a configuration you can use to build URLs for a
specific environment. Then have one version of this module file for
the dev environment, one for local and possibly one for production.
The beauty of Javascript is that you can include different
javascript files in the different environments, so the module could
be sourced from different files.
Have a look at this answer for more info on this option: https://stackoverflow.com/a/16340438/431522
Try to set the base url in master page header section like
<base href="/MyApp">
To differential between dev and release use conditional compilation. See here C# and ASP.NET MVC: Using #if directive in a view