通过在 Angular 上设置应用程序的路由和通过 Django 设置基本 URL,您可以轻松地将 Mezzanine 设置为在HTML5 模式下与 Angular 一起使用,确保任何未捕获的 URL 方案重定向到“home” URL:
在 Django 上:
# urls.py
urlpatterns = patterns("",
# Change the admin prefix here to use an alternate URL for the
# admin interface, which would be marginally more secure.
("^admin/", include(admin.site.urls)),
# If you'd like more granular control over the patterns in
# ``mezzanine.urls``, go right ahead and take the parts you want
# from it, and use them directly below instead of using
# ``mezzanine.urls``.
("^", include("mezzanine.urls")),
# AngularJS HTML5 mode (ie, remove the /#/ from URLs):
# We need to redirect any uncaught URL schemes to the default home view.
# http://scotch.io/quick-tips/js/angular/pretty-urls-in-angularjs-removing-the-hashtag
url(r'^.*$', TemplateView.as_view(template_name='index.html'), name='home'),
# etc...
)
在角上:
// app.js
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: '/static/app/views/home.html',
})
.when('/profile/:profileId', {
templateUrl: '/static/app/views/profile.html',
controller: 'ProfileCtrl'
})
.when('/results', {
templateUrl: '/static/app/views/results.html',
controller: 'ResultsCtrl'
})
.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
});
});
在 HTML 上:
<!-- index.html -->
<!doctype html>
<html class="no-js" lang="es" ng-app="myApp">
<head>
<base href="/">
<!-- etc -->
</head>
<!-- etc -->
</html>
相关博文在这里。