app.js
var app = angular.module('app', [
'ngResource',
'ngRoute',
// 'ui.bootstrap',
// 'ngResource',
'student',
]);
app.config(
function(
$locationProvider,
$routeProvider
){
$locationProvider.html5Mode({
enabled:true
})
$routeProvider.
when("/", {
template: 'base',
}).
when("/student/1", {
template: "<student-detail></student-detail>",
}).
otherwise({
template: "Not Found"
})
});
student.js
var app = angular.module('student', []);
app.component('studentDetail',{
templateUrl:'studentDetail.html',
controller: function($scope) {
$scope.test = 'Got it.'
}
});
urls.py
class SimpleStaticView(TemplateView):
def get_template_names(self):
return [self.kwargs.get('template_name') + ".html"]
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^api/', include("students.api.urls", namespace='students-api')),
url(r'^(?P<template_name>\w+)$', SimpleStaticView.as_view(), name='example'),
url(r'^$', TemplateView.as_view(template_name='home.html')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
测试,当访问/
,base
字段是出现的,说明ng-view
工作 正常,但当访问/students/1
时,返回django路由报错,未找到该路由。
studentDetail.html
是存在的。
这是angular没获取到路由请求吗?该如何解决?谢谢。
Thank you for the invitation, I recommend you read this article first - the core of single-page applications
When developing and debugging, you can use developer tools to check the actual path of the template request. In addition, for Django routing configuration, you only need to match the template request address and return the template file correctly. Please refer to the following example for the Angular 1.x front-end part:
Angular 1.x Demo project directory structure
views/student.module.js
views/studentDetail.html
index.html
It is recommended that if a new project uses Angular 1. Examples of components are as follows:
For details, please refer to component-property-binding-input-angular-2
In addition, if you are interested or the project allows, you can consider using the new version of Angular. The latest version is 4.0.1
Friendly reminder (please skip the question): This example needs to start the local server. If Python is installed, you can run python -m SimpleHTTPServer on the command line
References
Angularjs html5mode mode routing
Angular routing removes the # sign from the URL