Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Django URLs with AngularJs routeProvider

for a project, I am using Django on the back-end and AngularJs on the front end.

Basically, what I want is to run the Angular app only when the url starts with projectkeeper/.

In other words, lets say my website is example.com. I want the angular app to run for the URLs example.com/projectkeeper/dashboard/, example.com/projectkeeper/projects/ and so on, but not on example.com/about/.

Hope I have made myself clear. Anyway, in order to do this, I am doing the following with my code:

urls.py

urlpatterns = [
    url(r'^projectkeeper/$', TemplateView.as_view(template_name='base.html')),
]

In the template base.html, I refer to my angular app obviously. For the angular routing, I have done the following:

myapp.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/dashboard/', {
            title: 'Dashboard',
            controller : 'DashboardController',
            templateUrl : 'static/app_partials/projectkeeper/dashboard.html'
        })
        .otherwise({ redirectTo : '/' });
}]);

So, ideally, what I thought was that going to example.com/projectkeeper/#/dashboard/ would run the DashboardController from my angular app. However, this is not the case, I only get an empty page which means the routing was incorrect.

Any solutions to this? As I said before, I want is to run the Angular app only when the url starts with projectkeeper/.

like image 938
darkhorse Avatar asked Feb 06 '17 06:02

darkhorse


People also ask

What is $routeprovider in Angular 2?

$routeProvider is a simple API which which accepts either when () or otherwise () method. We need to install the ngRoute module. $routeProvider is a function under config (mainApp module) using the key as ‘$routeProvider’. $routeProvider.when defines the URL “/addStudent”.

How to add an angular route to a website?

Adding Angular Route ($routeProvider) 1 Reference to angular-route.js. This is a JavaScript file developed by Google that has all the functionality of routing. This needs to be placed in ... 2 The next important step is to add a dependency to the ngRoute module from within your application. This dependency is required so that routing ...

What is the use of ngroute in angular?

The ngRoute module which is developed by Google has all of the functionality which allows for routing to be possible. By adding this module, the application will automatically understand all of the routing commands. The $routeprovider is a service that angular uses to listen in the background to the routes which are called.

What is a default route in angular?

A default route can be set-up for angular.JS routing. It is used to determine what will be the default view to be shown to the user Parameters can be passed to the route via the URL as route parameters.


1 Answers

I think you're pretty close, there is few things to make this works(honestly, this is confusing to understand and I hope I didn't forget anything).

First, your URLs order is important, if you define something before the angular routes they will be rendered first, so use your django app's urls before the angular one.

Then, if you want to make angular know about the sub-path you need to define <base> tag in your header. In your case:

<base href="/projectkeeper/" />

(you can also define projectkeeperit in all of your where functions tho).

For the urls, I would change the regex to: r'^projectkeeper/.*'. Again, should be the last one in your urls list.

You will encounter other issues like the {{ }}, authentication issues, but those will stay for a different answer :)

like image 189
Or Duan Avatar answered Oct 23 '22 06:10

Or Duan