在配置$ routeProvider时是否可以从$ templateCache获取模板?

pau*_*eno 13 configuration angularjs

我的目的是通过一次调用外部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);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

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

Angular尝试home.html从服务器加载,因为$templateCache尚未填充.

假设all.json包含用于home.htmlmy-schedule.html流行示例的模板.

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

Raf*_*zak 6

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

我有grunt任务将我的所有JADE模板编译为HTML并将它们包装在一个大角度模块中,该模块作为一个名为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')
Run Code Online (Sandbox Code Playgroud)

主应用程序模块的开头(在这里使用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(...)
Run Code Online (Sandbox Code Playgroud)

GruntFile.json(grunt.js config)

我需要删除大约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']
Run Code Online (Sandbox Code Playgroud)

结果

单个.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>");
}]);
Run Code Online (Sandbox Code Playgroud)

链接

  1. grunt.js
  2. grunt html2js插件