AngularJS在django中开始加载部分,而不是在需要时加载
AngularJS load partials in the beginning and not at when needed in django?
我在django中有一个模板文件夹,我的应用程序的所有模板/部分都位于该文件夹中。我想在开始时将我的所有部分加载到angular的模板缓存中,而不是根据控制器的请求。
我有这样的路线设置:
var myApp = angular.module('myApp', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/landing', {
templateUrl: '/landing-partial',
controller: landingController
}).
when('/:wkspId/query', {
templateUrl: '/query-partial',
controller: queryController
}).
otherwise({
redirectTo: '/landing'
});
}]);
我想要类似于这个rails gem的解决方案(https://github.com/pitr/angular-rails-templates)在django。
根据您的构建系统,有一些插件可以为您解决这一问题。
对于GruntJS:https://www.npmjs.com/package/grunt-angular-templates
对于Gulp:https://www.npmjs.com/package/gulp-angular-templatecache
只要将它们指向你的html文件,它们就会创建一个javascript文件,看起来像:
angular.module('app').run(["$templateCache", function($templateCache) {
$templateCache.put("home.html",
// contents for home.html ...
);
并将从那里解析模板,而不是发出http请求来获取实际文件。你只需要把这个js文件和你的应用程序脚本标签一起包含进去,你就可以开始了。
您可以在index.html中插入模板,如下所示:
<script type="text/ng-template" id="/landing-partial">
Content of the template.
</script>
<script type="text/ng-template" id="/query-partial">
Content of the template.
</script>
之后,您可以在路由器中使用这些模板:
var myApp = angular.module('myApp', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/landing', {
templateUrl: '/landing-partial',
controller: landingController
}).
when('/:wkspId/query', {
templateUrl: '/query-partial',
controller: queryController
}).
otherwise({
redirectTo: '/landing'
});
}]);
它的工作原理与您模块提供的类似。在angular引导之后,模板将被放入$templateCache,并且可以在任何地方使用。
相关文章:
- django模板没有在AJAX发布后重新加载
- 没有在django项目中加载CSS文件
- Django设计模式-在加载时填充客户端JavaScript变量的方法
- 使用AJAX html()加载Django模板导致不需要的文本
- Django Tinymce JavaScript 未在非管理页面上加载
- Angular 和 django 无法加载 ng-bind
- 设置 URL 以在 Django 模板中加载 iframe
- 在 Django 中向下滚动加载数据
- 如何在 django 中加载页面之前自动滚动网页
- 如何在 ajax 加载表单上激活 django form.media javascript
- 在 Django 中页面重定向时显示加载
- Django 模板似乎没有以正确的顺序加载 Backbone.js
- 在 Web 应用程序加载时加载 JSON 响应(JQuery,Django)
- Django:使用框架的外部JS无法加载
- Django - 通过 AJAX 重新加载元素
- Django 在页面部分可用时动态加载它们
- 在 Django 中,根据视图名称动态加载一些 JavaScript 的更聪明的方法是什么(如果存在)
- 分别设置输出文件夹和文件 url;带有 Django 的 Webpack 文件加载器
- Django:如何为每个页面加载脚本
- 在Javascript变量中加载django自定义标签