AngularJS路由器不加载在`templateURL'上提供的页面 [英] AngularJS router does not load page served at `templateURL`

查看:443
本文介绍了AngularJS路由器不加载在`templateURL'上提供的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML页面(在访问路径 / 时获得服务):

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 这个:

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

我尝试过测试并提交表单。 URL更改为 / home ,但 TESTTTTTTT 不显示。当我通过地址栏手动将我的静态网址更改为 /static/templates/mainPage.html ,然后按Enter键, mainPage.html 其实是出现的。当我将 templateURL:'static / templates / mainPage.html'更改为 template:'< h5>这是默认路由< / 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.

如何进入 static / templates / mainPage.html 不显示?

我正在使用Django / DRF作为后台,这是我的 urls.py

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()),
]

这是我的 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")

请注意, / home 提供页面 mainPage.html / 提供页面 home.html (有点令人困惑我知道)。

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

推荐答案

不是templateUrl,而不是在您的$ routeProvider代码中的templateURL?

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

参考:

$ routeProvider文档

这篇关于AngularJS路由器不加载在`templateURL'上提供的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆