最近專案比較忙額,白天要上班,晚上回來還需要做Angular知識點的ppt給同事,畢竟年底要辭職了,專案的後續開發還是需要有人接手的,所以就佔用了晚上學習的時間。本來一直不打算寫這些第三方插件的學習筆記,不過覺得按需加載模組並且成功使用這個確實是個好處,還是記錄下來吧。基於本獸沒怎麼深入的使用requireJs,所以本獸不知道這個和requireJs有什麼區別,也不能清晰的說明這到底算不算Angular的按需加載。
為了實現這篇學習筆記知識點的效果,我們需要用到:
angular:https://github.com/angular/angular.js
ui-router:https://github.com/angular -ui/ui-router
ocLazyLoad:https://github.com/ocombe/ocLazyLoad
廢話不多說,進入正題...
首先我們看下文件結構:
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 --- 主页面
做嵌套路由,只是簡單實現單一視圖的路由以展示效果。
我們來看主頁面的程式碼:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <link rel="stylesheet" href="Scripts/bootstrap/dist/css/bootstrap.min.css" /> <script src="Scripts/angular-1.4.7/angular.js"></script> <script src="Scripts/angular-ui-router/release/angular-ui-router.min.js"></script> <script src="Scripts/oclazyload/dist/ocLazyLoad.min.js"></script> <script src="js/app.config.js"></script> <script src="js/oclazyload.config.js"></script> <script src="js/route.config.js"></script> </head> <body> <p ng-app="templateApp"> <p> <a href="#/default">主页</a> <a href="#/uibootstrap" >ui-bootstrap</a> <a href="#/ngtable">ng-table</a> <a href="#/ngtree">angular-tree-control</a> </p> <p ui-view></p> </p> </body> </html>
在這個頁面,我們只載入了bootstrap的css、angular的js、ui-router的js、ocLazyLoad的js,以及3個設定的js檔案。
再看看四個頁面的html代碼:
angular-tree-control效果頁面
<treecontrol tree-model="ngtree.treeData" class="tree-classic ng-cloak" options="ngtree.treeOptions"> {{node.title}} </treecontrol>
頁面上有個使用該插件對應的指令。
default頁面
<p class="ng-cloak"> {{default.value}} </p>
這裡我們只是用來證明載入並正確執行default.js。
ng-table效果頁
<p class="ng-cloak"> <p class="p-h-md p-v bg-white box-shadow pos-rlt"> <h3 class="no-margin">ng-table</h3> </p> <button ng-click="ngtable.tableParams.sorting({})" class="btn btn-default pull-right">Clear sorting</button> <p> <strong>Sorting:</strong> {{ngtable.tableParams.sorting()|json}} </p> <table ng-table="ngtable.tableParams" class="table table-bordered table-striped"> <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> </p>
這裡寫了一些簡單的ng-table效果。
ui-bootstrap效果頁面
<span uib-dropdown class="ng-cloak"> <a href id="simple-dropdown" uib-dropdown-toggle> 下拉框触发 </a> <ul class="uib-dropdown-menu dropdown-menu" 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.directive; 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 }); };
路由的配置:
"use strict" tempApp.config(["$stateProvider","$urlRouterProvider",routeFn]); function routeFn($stateProvider,$urlRouterProvider){ $urlRouterProvider.otherwise("/default"); $stateProvider .state("default",{ url:"/default", templateUrl:"views/default.html", controller:"defaultCtrl", controllerAs:"default", resolve:{ deps:["$ocLazyLoad",function($ocLazyLoad){ return $ocLazyLoad.load("js/controllers/default.js"); }] } }) .state("uibootstrap",{ url:"/uibootstrap", templateUrl:"views/ui-bootstrap.html", resolve:{ deps:["$ocLazyLoad",function($ocLazyLoad){ return $ocLazyLoad.load("ui.bootstrap"); }] } }) .state("ngtable",{ url:"/ngtable", templateUrl:"views/ng-table.html", controller:"ngTableCtrl", controllerAs:"ngtable", resolve:{ deps:["$ocLazyLoad",function($ocLazyLoad){ return $ocLazyLoad.load("ngTable").then( function(){ return $ocLazyLoad.load("js/controllers/ng-table.js"); } ); }] } }) .state("ngtree",{ url:"/ngtree", templateUrl:"views/angular-tree-control.html", controller:"ngTreeCtrl", controllerAs:"ngtree", resolve:{ deps:["$ocLazyLoad",function($ocLazyLoad){ return $ocLazyLoad.load("treeControl").then( function(){ return $ocLazyLoad.load("js/controllers/angular-tree-control.js"); } ); }] } }) };
ui-bootstrap的下拉框簡單的實作不需要控制器,那麼我們就只看看ng-table和angular-tree -control的控制器js吧:
ng-table.js
(function(){ "use strict" tempApp .controller("ngTableCtrl",["$location","NgTableParams","$filter",ngTableCtrlFn]); function ngTableCtrlFn($location,NgTableParams,$filter){ //数据 var data = [{ name: "Moroni", age: 50 }, { name: "Tiancum ", age: 43 }, { name: "Jacob", age: 27 }, { name: "Nephi", age: 29 }, { name: "Enos", age: 34 }, { name: "Tiancum", age: 43 }, { name: "Jacob", age: 27 }, { name: "Nephi", age: 29 }, { name: "Enos", age: 34 }, { name: "Tiancum", age: 43 }, { name: "Jacob", age: 27 }, { name: "Nephi", age: 29 }, { name: "Enos", age: 34 }, { name: "Tiancum", age: 43 }, { name: "Jacob", age: 27 }, { name: "Nephi", age: 29 }, { name: "Enos", age: 34 }]; this.tableParams = new NgTableParams( // 合并默认的配置和url参数 angular.extend({ page: 1, // 第一页 count: 10, // 每页的数据量 sorting: { name: 'asc' // 默认排序 } }, $location.search()) ,{ total: data.length, // 数据总数 getData: function ($defer, params) { $location.search(params.url()); // 将参数放到url上,实现刷新页面不会跳回第一页和默认配置 var orderedData = params.sorting ? $filter('orderBy')(data, params.orderBy()) :data; $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count())); } } ); }; })();
angular-tree-control.js
(function(){ "use strict" tempApp .controller("ngTreeCtrl",ngTreeCtrlFn); function ngTreeCtrlFn(){ //树数据 this.treeData = [ { id:"1", title:"标签1", childList:[ { id:"1-1", title:"子级1", childList:[ { id:"1-1-1", title:"再子级1", childList:[] } ] }, { id:"1-2", title:"子级2", childList:[ { id:"1-2-1", title:"再子级2", childList:[ { id:"1-2-1-1", title:"再再子级1", childList:[] } ] } ] }, { id:"1-3", title:"子级3", childList:[] } ] }, { id:"2", title:"标签2", childList:[ { id:"2-1", title:"子级1", childList:[] }, { id:"2-2", title:"子级2", childList:[] }, { id:"2-3", title:"子级3", childList:[] } ]} , { id:"3", title:"标签3", childList:[ { id:"3-1", title:"子级1", childList:[] }, { id:"3-2", title:"子级2", childList:[] }, { id:"3-3", title:"子级3", childList:[] } ] } ]; //树配置 this.treeOptions = { nodeChildren:"childList", dirSelectable:false }; }; })();
讓我們忽略default.js吧,畢竟裡面只有個"Hello Wrold"。
更多AngularJs 動態載入模組和依賴相關文章請關注PHP中文網!
相關文章:
🎜🎜AngularJS實現給動態產生的元素元素