废话不多说,进入正题...
首先我们看下文件结构:
Angular-ocLazyLoad --- demo文件夹 Scripts --- 框架及插件文件夹 angular-1.4.7 --- angular 不解释 angular-ui-router --- uirouter 不解释 oclazyload --- ocLazyload 不解释 bootstrap --- bootstrap 不解释 angular-tree-control-master --- angular-tree-control-master 不解释 ng-table --- ng-table 不解释 angular-bootstrap --- angular-bootstrap 不解释 js --- js文件夹 针对demo写的js文件 controllers --- 页面控制器文件夹 angular-tree-control.js --- angular-tree-control控制器代码 default.js --- default控制器代码 ng-table.js --- ng-table控制器代码 app.config.js --- 模块注册及配置代码 oclazyload.config.js --- 加载模块配置代码 route.config.js --- 路由配置及加载代码 views --- html页面文件夹 angular-tree-control.html --- angular-tree-control插件的效果页面 default.html --- default页面 ng-table.html --- ng-table插件效果页面 ui-bootstrap.html --- uibootstrap插件效果页面 index.html --- 主页面
注意:这个demo没做嵌套路由,只是简单实现单视图的路由以展示效果。
我们来看主页面的代码:
<html> <head> <meta charset="utf-8" /> <title></title> <link href="https://www.jb51.net/Scripts/bootstrap/dist/css/bootstrap.min.css" /> <script src="https://www.jb51.net/Scripts/angular-1.4.7/angular.js"></script> <script src="https://www.jb51.net/Scripts/angular-ui-router/release/angular-ui-router.min.js"></script> <script src="https://www.jb51.net/Scripts/oclazyload/dist/ocLazyLoad.min.js"></script> <script src="https://www.jb51.net/js/app.config.js"></script> <script src="https://www.jb51.net/js/oclazyload.config.js"></script> <script src="https://www.jb51.net/js/route.config.js"></script> </head> <body> <div ng-app="templateApp"> <div> <a href="#/default">主页</a> <a href="#/uibootstrap" >ui-bootstrap</a> <a href="#/ngtable">ng-table</a> <a href="#/ngtree">angular-tree-control</a> </div> <div ui-view></div> </div> </body> </html>
在这个页面,我们只加载了bootstrap的css、angular的js、ui-router的js、ocLazyLoad的js,以及3个配置的js文件。
再看看四个页面的html代码:
angular-tree-control效果页面
<treecontrol tree-model="ngtree.treeData" options="ngtree.treeOptions"> {{node.title}} </treecontrol>
页面上有个使用该插件对应的指令。
default页面
<div> {{default.value}} </div>
这里我们只是用来证明加载并正确执行default.js。
ng-table效果页面
<div> <div> <h3>ng-table</h3> </div> <button ng-click="ngtable.tableParams.sorting({})">Clear sorting</button> <p> <strong>Sorting:</strong> {{ngtable.tableParams.sorting()|json}} </p> <table ng-table="ngtable.tableParams"> <tr ng-repeat="user in $data"> <td data-title="'Name'" sortable="'name'"> {{user.name}} </td> <td data-title="'Age'" sortable="'age'"> {{user.age}} </td> </tr> </table> </div>
这里写了些简单的ng-table效果。
ui-bootstrap效果页面
<span uib-dropdown> <a href uib-dropdown-toggle> 下拉框触发 </a> <ul aria-labelledby="simple-dropdown"> 下拉框内容.这里写个效果证明实现动态加载即可 </ul> </span>
这里仅写了个下拉框效果,证明正确加载并使用该插件。
好了,看完了html,我们看下加载配置和路由配置:
"use strict" var tempApp = angular.module("templateApp",["ui.router","oc.lazyLoad"]) .config(["$provide","$compileProvider","$controllerProvider","$filterProvider", function($provide,$compileProvider,$controllerProvider,$filterProvider){ tempApp.controller = $controllerProvider.register; tempApp.directive = $compileProvider.register; tempApp.filter = $filterProvider.register; tempApp.factory = $provide.factory; tempApp.service =$provide.service; tempApp.constant = $provide.constant; }]);
以上代码对模块的注册,仅仅依赖了ui.router和oc.LazyLoad。配置也只是简单配置了模块,以便在后面的js能识别到tempApp上的方法。
然后我们再看看ocLazyLoad加载模块的配置:
"use strict" tempApp .constant("Modules_Config",[ { name:"ngTable", module:true, files:[ "Scripts/ng-table/dist/ng-table.min.css", "Scripts/ng-table/dist/ng-table.min.js" ] }, { name:"ui.bootstrap", module:true, files:[ "Scripts/angular-bootstrap/ui-bootstrap-tpls-0.14.3.min.js" ] }, { name:"treeControl", module:true, files:[ "Scripts/angular-tree-control-master/css/tree-control.css", "Scripts/angular-tree-control-master/css/tree-control-attribute.css", "Scripts/angular-tree-control-master/angular-tree-control.js" ] } ]) .config(["$ocLazyLoadProvider","Modules_Config",routeFn]); function routeFn($ocLazyLoadProvider,Modules_Config){ $ocLazyLoadProvider.config({ debug:false, events:false, modules:Modules_Config }); };
路由的配置: