【问题标题】:Is it possible to fetch templates from $templateCache when configuring my $routeProvider?配置我的 $routeProvider 时是否可以从 $templateCache 获取模板?
【发布时间】:2013-03-11 12:04:51
【问题描述】:

我的意图是通过一次调用包含所有模板名称和值列表的外部 JSON 文件来加载我的 Web 应用程序的所有模板。

我目前正在我的应用程序的运行阶段加载这些模板:

app.run(function ($http, $templateCache) {
    $http.get('/templates/all.json').success(function (data) {
        var i;
        for (i = 0; i < data.length; i++) {
            $templateCache.put(data[i].name, data[i].template);
        }
    });
});

但是,Angular.js 配置阶段在运行阶段之前执行,所以当我打算从 templateCache 加载时:

app.config(function($routeProvider, $locationProvider) {
    //App routing
    $routeProvider.
        //Homepage
        when('/', {templateUrl: 'home.html'}).

        //My schedule
        when('/my-schedule', {templateUrl: 'my-schedule.html'});
});

Angular 尝试从服务器加载 home.html,因为 $templateCache 还没有被填充。

假设all.json 包含上一个示例的home.htmlmy-schedule.html 的模板。

是否可以在配置应用的$routeProvider之前填写$templateCache

【问题讨论】:

  • 看看这个可能有帮助stackoverflow.com/questions/12346690/…
  • @Chandermani,虽然我的想法基于这个答案,但它并没有解决我的问题,即如何填写$templateCache before 配置我的$routeProvider
  • 我很遗憾错过了那部分:(
  • 你试过resolve回调$routeProvider吗?
  • @charlietfl 是的,我在玩resolve,但这不会让我依赖$routeProvider 配置中的templateUrl 属性,对吗?相反,我想我必须手动加载模板。

标签: configuration angularjs


【解决方案1】:

我遇到了同样的问题,经过几个小时的研究后,我意识到我问自己一个错误的问题,因为我想要做的不一定是使用 XHR 加载模板,而只是确保它们被缓存并存储在一个文件中

我有一项繁重的任务,将我所有的 JADE 模板编译为 HTML,并将它们包装在一个大的 Angular 模块中,该模块作为一个名为 templates.js 的单独 JS 文件加载。 所有这些事情都是在后台自动完成的,所以在你花 10 分钟配置它之后,你实际上可以忘记猴子的工作,专注于代码/标记。

(为简洁起见,我将跳过 JADE 部分)

  1. 编辑html文件
  2. grunt-contrib-watch 任务:
    1. 抓住变化
    2. 在启用 $templateCache 的情况下生成角度模块
  3. 我的网站已重新加载(使用 liveReload)

这就是我使用 Grunt.js / JADE 和 html2js 解决问题的方式:

我的 index.jade 文件的头部

  script(src='js/modernizr.js')
  script(src='js/vendor.js')
  script(src='js/templates.js')
  script(src='js/app.js')

主应用模块的开始(这里使用 CoffeeScript)

'use strict'
# Declare app level module which depends on filters, and services
App = angular.module('app', [ 
  'templates' // just include the module and forget it
  'foo'
  'bar']).config(...)

GruntFile.json(grunt.js 配置)

我需要删除大约 80% 的代码,只留下与模板相关的任务,将其视为草稿。

module.exports = (grunt)->
  imports = grunt.file.readJSON 'app/imports.json'
  grunt.initConfig
    pkg: grunt.file.readJSON 'package.json'



    watch:
      options:
        livereload: yes


      templates:
        files: 'app/**/*.jade'
        tasks: ['buildTemplates']

    jade:
      default:
        options:
          client: no
          wrap: no
          basePath: 'app/'
          pretty: yes
        files:
          'public/': ['app/**/*.jade']

    html2js:
      options:
        module: 'templates'
        base: 'public/'

      main:
        src: 'public/partials/**/*.html'
        dest: 'public/js/templates.js'


    connect:
      server:
        options:
          port: 8081
          base: 'public'
          keepalive: yes


      livereload:
        options:
          port: 8081
          base: 'public'
          # middleware: (connect, options)-> [lrSnippet, folderMount(connect, options.base)]

    copy:
      assets:
        files:[
          src:['**'], dest:'public/', cwd:'assets/', expand: yes
        ]



  grunt.loadNpmTasks 'grunt-contrib-concat'
  grunt.loadNpmTasks 'grunt-contrib-copy'
  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-contrib-clean'
  grunt.loadNpmTasks 'grunt-contrib-connect'
  grunt.loadNpmTasks 'grunt-contrib-compass'
  grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.loadNpmTasks 'grunt-contrib-livereload'
  grunt.loadNpmTasks 'grunt-jade'
  grunt.loadNpmTasks 'grunt-html2js'

  grunt.registerTask 'buildTemplates', ['clean:templates', 'copy:assets', 'jade', 'html2js:main', 'livereload']


  grunt.registerTask 'default', [ 'connect:livereload', 'watch']

结果

包含所有应用程序模板列表的单个 .js 文件,这些模板包含在与此类似的角度模块中:

angular.module("partials/directives/back-btn.html", []).run(["$templateCache", function($templateCache) {
  $templateCache.put("partials/directives/back-btn.html",
    "<a ng-href=\"{{href}}\" class=\"page-title-bar__back\"> <i class=\"icon-chevron-left\"> </i></a>");
}]);

链接

  1. grunt.js
  2. grunt html2js plugin

【讨论】:

    【解决方案2】:

    如果你使用 HTTP 拦截器,你可以在那里注入$templateCache

        $provide.factory('myHttpInterceptor', ['$templateCache', function($templateCache) {
            return {
                request: function(config) {
                    // do fancy things with $templateCache here
                    return config;
                }
            };
        }]);
    

    【讨论】:

    • 你能举一个更完整的例子吗? $provide 是从哪里来的?以及如何使用它...
    【解决方案3】:

    This Fiddle 是您所需要的。

    基本上 - 您可以将 $templateCache 键放在 templateUrl: 属性值中的 $routeProvider 配置中。

    【讨论】:

    • 不是他真正想要的。他想在路由提供者使用 templateUrl 加载模板之前预加载模板。我有同样的问题,我有模板存储在 CouchDB 中,不幸的是,AngularJS 不知道如何将 JSON 转换为 html 模板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多