有没有办法让AngularJS在开始时加载部分,而不是在需要时?

Ran*_*dra 190 angularjs

我有这样的路线设置:

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'
        });
}]);
Run Code Online (Sandbox Code Playgroud)

我希望能够让angularjs在开始时下载部分内容,而不是在请求时下载.

可能吗?

pko*_*rce 270

是的,至少有两个解决方案:

  1. 使用script指令(http://docs.angularjs.org/api/ng.directive:script)将您的部分放入最初加载的HTML中
  2. 如果需要,您也可以从JavaScript 填写$templateCache(http://docs.angularjs.org/api/ng.$templateCache)(可能基于$http调用结果)

如果您想使用方法(2)填写,$templateCache您可以这样做:

$templateCache.put('second.html', '<b>Second</b> template');
Run Code Online (Sandbox Code Playgroud)

当然模板内容可能来自一个$http电话:

$http.get('third.html', {cache:$templateCache});
Run Code Online (Sandbox Code Playgroud)

这里是plunker那些技术:http://plnkr.co/edit/J6Y2dc?p=preview

  • 我有时使用内联`<script>`标签和服务器端包含.这使我可以将我的部分文件作为单独的文件保存,以用于组织目的,但仍然可以在一个文档中提供所 (4认同)
  • 谢谢回复.我喜欢模板缓存Idea,因为我不想把东西放到脚本标签中.如何使用它?文档很糟糕.我在其中一条评论中看到了小提琴.但我想从网址加载. (2认同)
  • 你知道在选择一种解决方案与另一种解决方案时是否有提速? (2认同)
  • 我正在使用http调用缓存模板,但问题是缓存模板的名称将是url例如:$ http.get('app/correction/templates/group-correction-table.html',{cache:$ templateCache}); 然后每当我想加载模板时,我必须使用这个丑陋的名字:(我不喜欢,像这样:<td ng-include ="'app/correction/templates/group-correction-table.html' "> (2认同)

kar*_*old 25

如果你使用Grunt来构建你的项目,有一个插件可以自动将你的部分组合成一个Angular模块,该模块可以启动$ templateCache.您可以将此模块与其余代码连接起来,并在启动时从一个文件加载所有内容.

https://npmjs.org/package/grunt-html2js

  • 还有一个'grunt-angular-templates`也可以做同样的事情 - https://npmjs.org/package/grunt-angular-templates - 一种享受 (11认同)

Jam*_*ruk 16

添加构建任务以连接并注册Angular中的html部分$templateCache.(这个答案是karlgold答案的更详细变体.)

对于咕噜声,请使用grunt-angular-templates.对于gulp,请使用gulp-angular-templatecache.

下面是配置/代码片段来说明.

gruntfile.js示例:

ngtemplates: {
  app: {                
    src: ['app/partials/**.html', 'app/views/**.html'],
    dest: 'app/scripts/templates.js'
  },
  options: {
    module: 'myModule'
  }
}
Run Code Online (Sandbox Code Playgroud)

gulpfile.js示例:

var templateCache = require('gulp-angular-templatecache');
var paths = ['app/partials/.html', 'app/views/.html'];

gulp.task('createTemplateCache', function () {
return gulp.src(paths)
    .pipe(templateCache('templates.js', { module: 'myModule', root:'app/views'}))
    .pipe(gulp.dest('app/scripts'));
    });
Run Code Online (Sandbox Code Playgroud)

templates.js(此文件由构建任务自动生成)

$templateCache.put('app/views/main.html', "<div class=\"main\">\r"...
Run Code Online (Sandbox Code Playgroud)

的index.html

<script src="app/scripts/templates.js"></script>
<div ng-include ng-controller="main as vm" src="'app/views/main.html'"></div>
Run Code Online (Sandbox Code Playgroud)

  • 嗨@james 我正在使用这个 gulp 插件。所有 .html 都正确加载,但是当我尝试将 html 指定为某些对话框插件的一部分(我使用的是 ng-material)时,它会抛出 404。ex - $mdDialog.show({ controller: 'entityGridCtrl', templateUrl: 'user/ partials/entityGrid.html' }); (2认同)

Sim*_*ell 11

如果将每个模板包装在脚本标记中,例如:

<script id="about.html" type="text/ng-template">
<div>
    <h3>About</h3>
    This is the About page
    Its cool!
</div>
</script>
Run Code Online (Sandbox Code Playgroud)

将所有模板连接成一个大文件.如果使用Visual Studio 2013,请下载Web essentials - 它会添加一个右键菜单来创建HTML Bundle.

添加这个人编写的代码来改变角度$templatecache服务 - 它只是一小段代码而且有效:Vojta Jina的要点

$http.get应该更改为使用您的包文件:

allTplPromise = $http.get('templates/templateBundle.min.html').then(
Run Code Online (Sandbox Code Playgroud)

您的路线templateUrl应如下所示:

        $routeProvider.when(
            "/about", {
                controller: "",
                templateUrl: "about.html"
            }
        );
Run Code Online (Sandbox Code Playgroud)