我在 django 中有一个模板文件夹,其中包含我的应用程序的所有模板/部分。我想在开始时将所有部分加载到 Angular 模板缓存中,而不是根据 Controller 请求。
我的路线设置如下:
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'
});
}]);
我想要类似于 django 中的 Rails gem( https://github.com/pitr/angular-rails-templates ) 的解决方案。
最佳答案
根据您的构建系统,有一些插件可以为您处理这个问题。
对于 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 文件与您的应用程序脚本标记一起包含进去就可以了。
关于javascript - AngularJS 在 django 中开始加载部分而不是在需要时加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31020890/