AngularJS router does not load page served at `tem

2019-09-14 10:14发布

问题:

This is my HTML page (which gets served when accessing the path /):

<form ng-submit="ctrl.loginUser()" name="myLoginForm">
    <div class="form-group">
        <label>Username</label>
        <input type="text" name="uname" class="form-control" ng-model="ctrl.loginuser.username" required> 
    </div>

    <div class="form-group">
        <label>Password</label>
        <input type="password" name="pwd" class="form-control" ng-model="ctrl.loginuser.password" required>
    </div>

    <input type="submit" value="Login"> 
</form>

<div ng-view></div>

And this is what gets called when the form is submitted:

angular.module("BaseApp", [])
    .config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.xsrfCookieName = 'csrftoken';
        $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
    }])

    .config(['$locationProvider', function($locationProvider){
        $locationProvider.html5Mode(true);
    }])

.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/home', {
    templateURL: 'static/templates/mainPage.html'
})
/*
.otherwise({redirectTo: '/'});
*/
}])


self.loginUser = function(loginuser) {
    return $http.post("/custom-api-auth/login", loginuser)
    .then(function(response) {
        $location.url("/home");
    });
};

static/templates/mainPage.html is this:

<html>
<head>
<base href="/" >
</head>
<h5>TESTTTTTTTT</h5>

I tried testing this and submitting the form. The URL changes to /home, but TESTTTTTTTT doesn't show up. When I manually change my static URL to /static/templates/mainPage.html through the address bar and then hit enter, mainPage.html does in fact appear. When I change templateURL: 'static/templates/mainPage.html' to template: '<h5>This is the default route</h5>', This is the default route does show up correctly.

How come static/templates/mainPage.html does not show up?

I am using Django / DRF as my backend, and this is my urls.py:

from django.conf.urls import url
from django.conf.urls import include
from ebdjangoapp import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register(r'stuffs', views.StuffViewSet)
router.register(r'users', views.UserViewSet)

urlpatterns = [
    url(r'^api/', include(router.urls)),

    # templates
    url(r'^$', views.HomePageView.as_view()),
    url(r'^home$', views.MainPageView.as_view()),

    # login logout
    url(r'^custom-api-auth/login$', views.login_view.as_view()),
    url(r'^custom-api-auth/logout$', views.logout_view.as_view()),
]

And this is my views.py:

class HomePageView(TemplateView):
    def get(self, request, *args, **kwargs):
        return TemplateResponse(request, "home.html")

class MainPageView(TemplateView):
    def get(self, request, *args, **kwargs):
        return TemplateResponse(request, "mainPage.html")

Note that /home serves the page mainPage.html and / serves the page home.html (a bit confusing I know).

回答1:

Isn't it templateUrl, not templateURL in your $routeProvider code?

Reference:
$routeProvider documentation