Angular1.x自定義指令實(shí)例詳解
本文實(shí)例講述了Angular1.x自定義指令。分享給大家供大家參考,具體如下:
調(diào)用Module.directive方法,傳入指令名稱和工廠函數(shù),返回一個對象。
指令名稱中每個大寫字母會被認(rèn)為是屬性名中的一個獨(dú)立的詞,而每個詞之間是以一個連字符分隔的。
var myApp = angular.module('myApp', []) .directive("unorderedList", function () { return function(scope, element, attrs) { }; });
返回鏈?zhǔn)胶瘮?shù)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AngularJS Demo</title> <link rel="stylesheet" href="../css/bootstrap.css" rel="external nofollow" /> <link rel="stylesheet" href="../css/bootstrap-theme.css" rel="external nofollow" > <script src="../js/angular.js"></script> </head> <body ng-app="myApp" ng-controller="myCtrl"> <div class="panel panel-default"> <div class="panel-heading"> <h3>Products</h3> </div> <div class="panel-body"> <div unordered-list="products"></div> </div> </div> </body> <script> var myApp = angular.module('myApp', []) .controller('myCtrl', ["$scope", function ($scope) { $scope.products = [ { name: "Apples", category: "Fruit", price: 1.20, expiry: 10 }, { name: "Bananas", category: "Fruit", price: 2.42, expiry: 7 }, { name: "Pears", category: "Fruit", price: 2.02, expiry: 6 } ]; }]) .directive("unorderedList", function () { return function (scope, element, attrs) { var data = scope[attrs['unorderedList']]; if( angular.isArray(data) ){ for(var i=0, len=data.length; i<len; i++){ console.log(data[i].name); } } }; }); </script> </html>
打破對數(shù)據(jù)屬性的依賴
設(shè)置一個元素屬性,用來動態(tài)第設(shè)置需要參加運(yùn)算的鍵。如果屬性名是以data-為前綴的,AngularJS會在生成傳給連接函數(shù)的屬性集合時(shí)移除這一前綴。也就是說data-list-property和list-property都會被表示為listProperty。
<div unordered-list="products" list-property="name"></div>
var data = scope[attrs['unorderedList']]; var propertyName = attrs['listProperty']; if(angular.isArray(data)){ var listElem = angular.element("<ul>"); element.append(listElem); for(var i=0, len=data.length; i<len; i++){ listElem.append( angular.element('<li>').text(data[i][propertyName]) ); } }
計(jì)算表達(dá)式
<div unordered-list="products" list-property="price | currency"></div>
添加過濾器后,自定義指令被破壞了??梢宰屪饔糜?qū)傩灾诞?dāng)做一個表達(dá)式進(jìn)行計(jì)算。scope.$eval()接收兩個參數(shù):要計(jì)算的表達(dá)式和需要用于執(zhí)行該計(jì)算的任意本地?cái)?shù)據(jù)。
listElem.append( angular.element('<li>').text(scope.$eval(propertyName, data[i])) );
處理數(shù)據(jù)變化
<div class="panel-body"> <button class="btn btn-primary" ng-click="incrementPrices()"> Change Prices </button> </div>
$scope.incrementPrices = function () { for(var i=0,len = $scope.products.length; i<len; i++){ $scope.products[i].price++; } }
添加監(jiān)聽器
if(angular.isArray(data)){ var listElem = angular.element("<ul>"); element.append(listElem); for(var i=0, len=data.length; i<len; i++){ var itemElem = angular.element('<li>'); listElem.append(itemElem); var watchFn = function (watchScope) { return watchScope.$eval(propertyName, data[i]); }; scope.$watch(watchFn, function (newValue, oldValue) { itemElem.text(newValue); }); } }
第一個函數(shù)(監(jiān)聽器函數(shù))基于作用域中的數(shù)據(jù)計(jì)算出一個值,該函數(shù)在每次作用域發(fā)生變化時(shí)都會被調(diào)用。如果該函數(shù)的返回值發(fā)生了變化,處理函數(shù)就會被調(diào)用,這個過程就像字符串表達(dá)式方式一樣。提供一個函數(shù)來監(jiān)聽,能夠從容地面對表達(dá)式中有可能帶有過濾器的數(shù)據(jù)值得情形。
第二個監(jiān)聽器函數(shù)是針對$eval()
計(jì)算的表達(dá)變化,來執(zhí)行回調(diào)函數(shù)的。
以上代碼并不能正確顯示,涉及到for循環(huán)閉包問題。
for(var i=0, len=data.length; i<len; i++){ (function () { var itemElem = angular.element('<li>'); listElem.append(itemElem); var index = i; var watchFn = function (watchScope) { return watchScope.$eval(propertyName, data[index]); }; scope.$watch(watchFn, function (newValue, oldValue) { itemElem.text(newValue); }); }()); }
或者
(function (i) { var itemElem = angular.element('<li>'); listElem.append(itemElem); var watchFn = function (watchScope) { return watchScope.$eval(propertyName, data[i]); }; scope.$watch(watchFn, function (newValue, oldValue) { itemElem.text(newValue); }); })(i);
jqLite
jqLite中element()
、append()
等方法的參數(shù)是HTML string or DOMElement。
return function (scope, element, attrs) { var listElem = element.append("<ol>"); for (var i = 0; i < scope.names.length; i++) { listElem.append("<li>").append("<span>").text(scope.names[i]); } }
上述添加的是字符串,最后添加只有scope.names中最后一條信息。
return function (scope, element, attrs) { var listElem = angular.element("<ol>"); element.append(listElem); for (var i = 0; i < scope.names.length; i++) { listElem.append(angular.element("<li>") .append(angular.element("<span>").text(scope.names[i]))); } }
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解Angular系列之變化檢測(Change Detection)
這篇文章主要介紹了詳解Angular系列之變化檢測(Change Detection),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02angularJs自定義過濾器實(shí)現(xiàn)手機(jī)號信息隱藏的方法
今天小編就為大家分享一篇angularJs自定義過濾器實(shí)現(xiàn)手機(jī)號信息隱藏的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10AngularJS動態(tài)綁定ng-options的ng-model實(shí)例代碼
本篇文章主要介紹了AngularJS動態(tài)綁定ng-options的ng-model實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06深入學(xué)習(xí)JavaScript的AngularJS框架中指令的使用方法
這篇文章主要介紹了深入學(xué)習(xí)JavaScript的AngularJS框架中指令的使用方法,指令的使用是Angular入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-03-03AngularJS全局scope與Isolate scope通信用法示例
這篇文章主要介紹了AngularJS全局scope與Isolate scope通信用法,結(jié)合格式分析了全局scope和directive本地scope相關(guān)功能、通信用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-11-11詳解Angular-ui-BootStrap組件的解釋以及使用
這篇文章主要介紹了詳解Angular-ui-BootStrap組件的解釋以及使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07swiper在angularjs中使用循環(huán)輪播失效的解決方法
今天小編就為大家分享一篇swiper在angularjs中使用循環(huán)輪播失效的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09