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. java获取服务器所有信息

    package com.sinosoft.outher.listener; import java.net.InetAddress;import java.net.UnknownHostExcepti ...

  2. Android开发--微信布局(ListView)基本运用

    ListView 1.ListVeiw 用来展示列表的View. 2.适配器 用来把数据映射到ListView上的中介. 3.数据    具体的将被映射的字符串,图片,或者基本组件. 根据列表的适配器 ...

  3. 说一说inline-block的奇葩之处

    今天本来想聊一下margin和padding,但是当我给div加了一个display:inline-block之后,发现一个问题: .box_demo{border: 1px solid #333;w ...

  4. XGBoost参数调优完全指南(附Python代码)

    XGBoost参数调优完全指南(附Python代码):http://www.2cto.com/kf/201607/528771.html https://www.zhihu.com/question/ ...

  5. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

  6. linux Bash

    本文包含的命令:type.echo.evn.set.locale.read.declare / typeset.ulimit.alias.unalias.history.!.source.stty.c ...

  7. ajax头像上传

    html代码: <input id="fileinput" type="file" /><br /> <br /> < ...

  8. 第四十二章 微服务CICD(4)- jenkins + gitlab + webhooks + publish-over-ssh(2)

    上一节完成了"当git客户端push代码到gitlab后,jenkins会立即去gitlab拉取代码并构建". 目的:本节完成jenkins自动构建之后,自动的将jar包部署到应用 ...

  9. nginx解析php请求为404

    刚搭建了lnmp环境,发现如果讲我的程序放在某个文件夹的时候,然后nginx进行代理的时候竟然会是404: nginx配置如下: # pass the PHP scripts to FastCGI s ...

  10. 解决: Can’t connect to local MySQL server through socket /var/lib/mysql/mysql.sock

    今天在搬瓦工上使用mysql 命令行,总报一个这样的错:Can't connect to local MySQL server through socket '/xxxxx/mysql.sock',一 ...