配置 – 在配置$routeProvider时可以从$templateCache获取模板吗?

前端之家收集整理的这篇文章主要介绍了配置 – 在配置$routeProvider时可以从$templateCache获取模板吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的目的是将我的网络应用程序的所有模板加载到包含所有模板名称和值的列表的外部 JSON文件中.

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

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

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

  1. app.config(function($routeProvider,$locationProvider) {
  2. //App routing
  3. $routeProvider.
  4. //Homepage
  5. when('/',{templateUrl: 'home.html'}).
  6.  
  7. //My schedule
  8. when('/my-schedule',{templateUrl: 'my-schedule.html'});
  9. });

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

假设all.json包含了home.html和my-schedule.html的模板,这个例子就是这个例子.

在配置应用程序的$routeProvider之前,可以填写$templateCache吗?

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

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

(为了简洁起见,我将在这里跳过JADE部分)

>编辑html文件
grunt-contrib-watch任务:

抓住变化
>使用$templateCache生成角度模块

>我的网站被重新加载(使用liveReload)

这是我如何使用Grunt.js / JADE和html2js来解决问题:

我的index.jade文件的头部分

  1. script(src='js/modernizr.js')
  2. script(src='js/vendor.js')
  3. script(src='js/templates.js')
  4. script(src='js/app.js')

主要应用程序模块的开始(在这里使用CoffeeScript)

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

GruntFile.json(grunt.js config)

我需要删除大约80%的代码,只剩下与模板相关的任务,只考虑一个草稿.

  1. module.exports = (grunt)->
  2. imports = grunt.file.readJSON 'app/imports.json'
  3. grunt.initConfig
  4. pkg: grunt.file.readJSON 'package.json'
  5.  
  6.  
  7.  
  8. watch:
  9. options:
  10. livereload: yes
  11.  
  12.  
  13. templates:
  14. files: 'app/**/*.jade'
  15. tasks: ['buildTemplates']
  16.  
  17. jade:
  18. default:
  19. options:
  20. client: no
  21. wrap: no
  22. basePath: 'app/'
  23. pretty: yes
  24. files:
  25. 'public/': ['app/**/*.jade']
  26.  
  27. html2js:
  28. options:
  29. module: 'templates'
  30. base: 'public/'
  31.  
  32. main:
  33. src: 'public/partials/**/*.html'
  34. dest: 'public/js/templates.js'
  35.  
  36.  
  37. connect:
  38. server:
  39. options:
  40. port: 8081
  41. base: 'public'
  42. keepalive: yes
  43.  
  44.  
  45. livereload:
  46. options:
  47. port: 8081
  48. base: 'public'
  49. # middleware: (connect,options)-> [lrSnippet,folderMount(connect,options.base)]
  50.  
  51. copy:
  52. assets:
  53. files:[
  54. src:['**'],dest:'public/',cwd:'assets/',expand: yes
  55. ]
  56.  
  57.  
  58.  
  59. grunt.loadNpmTasks 'grunt-contrib-concat'
  60. grunt.loadNpmTasks 'grunt-contrib-copy'
  61. grunt.loadNpmTasks 'grunt-contrib-coffee'
  62. grunt.loadNpmTasks 'grunt-contrib-clean'
  63. grunt.loadNpmTasks 'grunt-contrib-connect'
  64. grunt.loadNpmTasks 'grunt-contrib-compass'
  65. grunt.loadNpmTasks 'grunt-contrib-watch'
  66. grunt.loadNpmTasks 'grunt-contrib-livereload'
  67. grunt.loadNpmTasks 'grunt-jade'
  68. grunt.loadNpmTasks 'grunt-html2js'
  69.  
  70. grunt.registerTask 'buildTemplates',['clean:templates','copy:assets','jade','html2js:main','livereload']
  71.  
  72.  
  73. grunt.registerTask 'default',[ 'connect:livereload','watch']

结果

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

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

链接

> grunt.js
> grunt html2js plugin

猜你在找的Angularjs相关文章