微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有使用AngularJS路由预加载模板的方法吗?

加载Angular应用程序后,我需要一些离线可用的模板。

这样的东西是理想的:

$routeProvider
  .when('/p1',{
    controller: controller1,templateUrl: 'Template1.html',preload: true
  })
一个模板缓存服务: $templateCache,可以用来在javascript模块中预加载模板。

例如,从文档:

var myApp = angular.module('myApp',[]);
  myApp.run(function($templateCache) {
  $templateCache.put('templateId.html','This is the content of the template');
});

甚至有一个grunt任务从html文件生成一个javascript模块:grunt-angular-templates

另一种方式,也许不太灵活,是使用内联模板,例如,在index.html中有一个类似这样的脚本标签

<script type="text/ng-template" id="templates/Template1.html">template content</script>

意味着模板可以在后面以与路由配置中的真实url相同的方式进行编址(templateUrl:’templates / Template1.html’)

原文地址:https://www.jb51.cc/angularjs/146230.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐