1、在lib目录中添加 script.js 文件,并在index.html其他<script>之前引用之:

<script src="lib/script.js"></script>
 
2、在app.js中增加全局函数LazyLoadTemplate()和LazyLoadJs():
function LazyLoadTemplate(file) {
return function () {
return file;
}
} function LazyLoadJs(file) {
return {
deps: function ($q, $rootScope) {
var d = $q.defer();
$script(file, function () {
$rootScope.$apply(function () {
d.resolve();
})
});
return d.promise;
}
}
}
3、在app.config()函数中开始处增加:
var app = angular.module("app");
app.controllerProvider = $controllerProvider; // 主要是这个
app.compileProvider = $compileProvider; // 以下这两个备用
app.filterProvider = $filterProvider;
注意,要在config()的参数列表中增加相应的$controllerProvider、$compileProvider 和 $filterProvider。
 
4、在路由表中修改需要动态加载的状态路由配置,例如:
$stateProvider.state('page1', {
url: '/page1',
templateUrl: LazyLoadTemplate('page1.html'),
controller: 'Page1Ctrl',
resolve: LazyLoadJs("page1.js")
});
提示:
(1) 把 templateUrl属性的值改为调用函数 LazyLoadTemplate()。
(2) 增加 resolve属性,其值为调用函数LazyLoadJs()。
 
5、在控制器所在JS文件中,修改控制器的定义方式:
angular.module('app').controllerProvider.register("Page1Ctrl", function ($scope) {
$scope.title = "Page1";
});
提示:把原来的 controller() 函数调用改成  controllerProvider.register()。
 
6、从index.html中去掉对该控制器所在JS文件的引用,例如:
<!--<script src="page1.js"></script>-->
 
 
参考:
 

对angular实现延迟加载template和controller的更多相关文章

  1. AngularJS 特性—SinglePage、template、Controller

    单页Web应用(SinglePage) 顾名思义,只使用一个页面的Web应用程序.单页面应用是指用户通过浏览器加载独立的HTML页面,Ajax加载数据页面无刷新,实现操作各种操作. 模板(templa ...

  2. angular 自定义指令 link or controller

    Before compilation? – Controller After compilation? – Link var app = angular.module('plunker', []); ...

  3. [Angular 2] Create template with Params

    Angular 2 templates have a special let syntax that allows you to define and pass a context when they ...

  4. [Angular 2] Passing Template Input Values to Reducers

    Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...

  5. angular学习(四)-- Controller

    1.4 控制器:Controller ng 中的控制器用来对 scope 进行操作 包括初始化数据和定义事件响应函数等 ng 用来解耦业务逻辑层和视图层的关键 controller 操作 scope, ...

  6. angular.js--demo2-----声明局部控制器controller

    <!doctype html><html ng-app="HelloAngular"> <head> <meta charset=&quo ...

  7. angular ajax的使用及controller与service分层

    一个简单的例子,控制层:.controller('publishController',['$scope','publishService', function($scope,publishServi ...

  8. [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>

    Angular 1 provided a mechanism to place content from your template inside of another template called ...

  9. [Angular] Test component template

    Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...

随机推荐

  1. VS2010+VMWare8+VisualDDK1.5.6 创建并调试你的第一个驱动程序 - 完全教程

    本文描述了如何使用Visual Studio+VMMare+VisualDDK来创建.编译和调试你的第一个驱动程序.本文提供在开发和调试工具的环境下详细的操作步骤,而无需太多的关心这些环境背后所做的事 ...

  2. Android软件测试Monkey测试工具

    前言: 最近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框架.CTS.Monkey.Monkeyrunner.benchmark.其 ...

  3. Python复习笔记-字典和文件操作

    抽时间回顾2年前自己做过的python工具,突然感觉不像自己写的,看来好久没用过python的字典和文件操作了,查询资料和网页,整理如下: 一.字典 键值对的集合(map) 字典是以大括号“{}”包围 ...

  4. ini 文件

    INI文件是一个无固定标准格式的配置文件.它以简单的文字与简单的结构组成,常常使用在视窗操作系统,或是其他操作系统上,许多程序也会采用INI文件做为设置程序之用.视窗操作系统后来以注册表的形式替换掉I ...

  5. 【Tree 3】树形结构数据加载的思考

    前面两篇文章,分别介绍了使用递归和非递归算法加载树形结构数据的方式,本篇文章,则是自己闲下来的时候,进行的一点小思考. 一.什么地方会用到树形结构 刚开始一看到这种结构的时候,最先是想到了家谱.家谱就 ...

  6. go特性学习

    而后我写了一些测试代码: dept1 :=     Dept{         name:     "MySohu",         building: "Intern ...

  7. 什么是Mbps、Kbps、bps、kb、mb及其换算和区别

    Mbps 即 Milionbit pro second(百万位每秒): Kbps 即 Kilobit pro second(千位每秒): bps 即 bit pro second(位每秒): 速度单位 ...

  8. NHibernate系列文章十:NHibernate对象二级缓存下

    摘要 上一节对NHibernate二级缓存做了简单介绍,NHibernate二级缓存是由SessionFactory管理的,所有Session共享.这一节介绍二级缓存其他两个方面:二级缓存查询和二级缓 ...

  9. 项目名 的在JSP或JAVA中的另类写法

    在JSP页面中${pageContext.request.contextPath } 表示项目名<form action="${pageContext.request.contextP ...

  10. 如何进行服务器的批量管理以及python 的paramiko的模块

    最近对公司的通道机账号进行改造管理,全面的更加深入的理解了公司账号管理的架构.(注:基本上所有的机器上的ssh不能使用,只有部分机器能够使用.为了安全的角度考虑,安装的不是公版的ssh,而都是定制版的 ...