anjularjs-filter模組化
1、過濾器
<!doctype html><!-- ng-app使用範圍HTML --><html lang="en" ng-app="test"> <head> <meta <meta charset=" " set="UTF-8"> <title>Document</title> <style type="text/css" media="screen"> #div1 input {background: #CCC;} #div1 input.active {background: yellow;} #div1 div {width: 200px;height: 200px;border: 1px solid #000;} </style> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="angular.min.js"></script> <script type="text/javascript"> var app=angular.module('test', []) app.controller('cont1', function($scope){ $scope.arr=[12.4,12,3,44] $scope.timer=12341341234 }) app.filter('my_filter',function () { //只執行一次,進行初始化 //alter() return function (input) { //執行多次 return input+19 } }) </script> <style type="text/css" media="screen"> </style></head><body ng-controller="cont1"> <ul> <li ng-repeat="v in arr">{{v|my_filter}}:{{timer|date:'yyyy年MM月dd日'}}</li> </ul></body></html>
2、directive:增強HTML
可以使用 .directive 函式來新增自定義的指令。
要呼叫自定義指令,HTML 元素上需要新增自定義指令名。
使用駝峰法來命名一個指令, runoobDirective, 但在使用它時需要以 - 分割, runoob-directive:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="angular.min.js"></script> <script type="text/javascript"> var app=angular.module('test', []) app.directive("runoobDirective", function() { return { restrict : 'C', template : "<h1>自定義指令!</h1>" // replace : true // M 必須加上replace:true,替換原有元素;replace:false,插入標籤 }; }); </script></head><body ng-app="test"> <runoob-directive>restrict:E</runoob-directive> <div runoob-directive>restrict:A</div> <span class="runoob-directive">restrict:C</span> <!-- directive: runoob-directive --></body></html>
restrict 值可以是以下幾種:
E
作為元素名使用A
作為屬性使用C
作為類名使用M
作為註釋使
restrict 預設值為 EA
, 即可以透過元素名和屬性名來呼叫指令。
3、directive>ng-transclude 嵌入 佔位符
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="angular.min.js"></script> <script type="text/javascript"> var app=angular.module('test', []) app.directive("runoobDirective", function() { return { restrict : 'E', template : "<h1><ng-transclude></ng-transclude> 自定義指令!</h1>", transclude:true // replace : true // M 必須加上replace:true,替換原有元素;replace:false,插入標籤 }; }); </script></head><body ng-app="test"> <runoob-directive>restrict:E</runoob-directive> <div runoob-directive>restrict:A</div> <span class="runoob-directive">restrict:C</span> <!-- directive: runoob-directive --></body></html>
4、directive-下拉框組建
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="angular.min.js"></script> <script type="text/javascript"> var app=angular.module('test', []) app.controller('cont1', ['$scope', function ($scope) { $scope.str="" $scope.arr=['iphone','huawei','meizu','sansunm','lenovo'] }]) app.directive('drop', [function () { return { restrict: 'E', template: '<input type="text" ng-model="str"> <ul> <li ng-repeat="a in arr" ng-show="a.indexOf(str)!=-1" >`a`</li> </ul>' }; }]) </script></head><body ng-app='test' ng-controller='cont1'> <drop></drop></body></html>
5、angular模組化技術
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="angular.min.js"></script> <script type="text/javascript"> //angular.module() 建立模組mod1 angular.module('mod1', []).controller('mod1Ctrl', ['$scope', function ($scope) { $scope.a="mod1Ctrl" }]) //angular.module() 建立模組mod2 angular.module('mod2', []).controller('mod2Ctrl', ['$scope', function ($scope) { $scope.b="mod2Ctrl" }]) ////angular.module() 建立模組mod3,呼叫其他模組mod1,mod2 angular.module('mod3', ['mod1','mod2']) </script></head><!-- ng-app 使用模組mod3 --><body ng-app='mod3' > <div ng-controller="mod1Ctrl">`a`</div> <div ng-controller="mod2Ctrl">`b`</div> <div>`a`,`b`</div></body></html>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1795/viewspace-2815664/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 序列化模組,隨機數模組,os模組,sys模組,hashlib模組隨機
- Android模組化改造以及模組化通訊框架Android框架
- 序列化模組,subprocess模組,re模組,常用正則
- JavaScript模組化JavaScript
- 前端模組化前端
- JS模組化JS
- ES6 模組化與 CommonJS 模組化區別JS
- Python常用模組(random隨機模組&json序列化模組)Pythonrandom隨機JSON
- Android模組化與元件化–多模組區分編譯Android元件化編譯
- 分而治之-前端模組化前端
- Javascript 模組化指北JavaScript
- css模組化方案CSS
- 淺談模組化
- Webpack之模組化優化Web優化
- iOS的元件化(模組化)之路iOS元件化
- CSS 如何模組化,工程化CSS
- 前端模組化規範前端
- AppDelegate模組化歷程APP
- 模組化日常:重名類
- javascript模組化簡介JavaScript
- 淺談前端模組化前端
- ES6 - 模組化
- 前端模組化基礎前端
- JS模組化系統JS
- Vue 路由模組化配置Vue路由
- 前端模組化的前世前端
- JavaScript 模組化前世今生JavaScript
- 01模組化簡介
- JavaScript 中的模組化JavaScript
- ES6模組化
- 模組化開發(二)
- JavaScript 模組化總結JavaScript
- GTID模組初始化
- AppDelegate的模組化+瘦身APP
- 前端模組化雜記前端
- JS模組化規範JS
- JavaScript模組化規範JavaScript
- 前端模組化彙總前端