AngularJS使用自定義指令替代ng-repeat的方法
前言
大家都知道對(duì)于處理小數(shù)量,ng-repeat是非常有用的,但是如果需要處理非常大的數(shù)量集,還是采用自定義的方法更好一些。特別是數(shù)據(jù)大多都是靜態(tài)的或已預(yù)存儲(chǔ)好的,這個(gè)時(shí)候應(yīng)避免使用ng-repeat指令。
ng-repeat中的表達(dá)式和 $watch
Angular中的表達(dá)式都會(huì)創(chuàng)建$watch
的Scope
函數(shù)。用于監(jiān)聽(tīng)模型變化,當(dāng)你的模型部分發(fā)生變化時(shí)它會(huì)通知你。在ng-repeat指令中,如果某行數(shù)據(jù)有15列數(shù)據(jù)都綁定了表達(dá)式,如果數(shù)據(jù)有1000多行的話,那么$watch
就又獎(jiǎng)金15000個(gè),這性能簡(jiǎn)直難以想象。
所以當(dāng)我們想要實(shí)現(xiàn)ng-repeat的功能又想兼?zhèn)湫阅?,那只能另找一種方法了。
替換ng-repeat的方法
如果內(nèi)容是靜態(tài)的,我們不需要兩種方式的綁定,只需要執(zhí)行一次賦值語(yǔ)句{{::value}}
就可以。如果anguluarJS是1.3以下的舊版本,是不支持的一次性綁定語(yǔ)法的。那么最好的方法就是自定義指令,換言之,靜態(tài)數(shù)據(jù)可以使用一些簡(jiǎn)單的方法來(lái)格式化。
實(shí)現(xiàn)步驟
1、首先創(chuàng)建無(wú)序列表,用于保存動(dòng)態(tài)綁定的內(nèi)容。
創(chuàng)建UL標(biāo)簽作為容器用于顯示列表
我們選擇動(dòng)態(tài)加載List中的數(shù)據(jù),首先添加div標(biāo)簽,并命名為"repeater-alternative"用于渲染流中。
<div> <ul> <div repeater-alternative></div> </ul> </div>
2、定義List 數(shù)據(jù):
//示例數(shù)據(jù) var studentsList = [ { FirstName: "Raj, LastName : "B", Country : "India", BirthDate: "01/01/1990" }, { FirstName: "Kumar, LastName : "S", Country : "India", BirthDate: "01/01/1990" }, .................. .................. .................. .................. ]; $scope.collectionObject = studentsList; //分配給$scope函數(shù)
3、實(shí)際List內(nèi)容
主要目的適用于重復(fù)集合對(duì)象,并顯示到列表中,所以需要制定訪問(wèn)循環(huán)的邏輯,并按照需求來(lái)格式化字符串。
var tableRow = ""; angular.forEach($scope.collectionObject, function (item) { tableRow = tableRow + ['<li>', '<div class="col-md-1">' + item.FirstName + '</div> ', '<div class="col-md-1 ">' + item.LastName + '</div> ', '<div class="col-md-1 ">' + item.Country+ '</div> ', '<div class="col-md-1 ">' + item.Id + '</div> ', '<div class="col-md-1 ">' + $filter('date')(item.BirthDate, 'dd-MMM-yyyy') + '</div> ', '</li>'].join(''); });
4、List格式化邏輯
一旦collectionObject
的值已被賦給其他變量,就需要定義顯示數(shù)據(jù)的表格。
5、如何獲取分配CollectionObject的時(shí)間
Angular會(huì)監(jiān)控$scope
變量值得改變,一旦值被修改,則$watch
將被處罰,所以需要將CollectionObject
賦值邏輯放到$scope
變量的$watch
中。
代碼如下:
$scope.$watch($scope.object, function (oldValue, newValue) { })
即,當(dāng)我們執(zhí)行賦值語(yǔ)句是,Angular會(huì)處理這個(gè)事件,并格式化List
的內(nèi)容。
$scope.$watch('collectionObject', function (oldValue, newValue) { var tableRow = ""; angular.forEach($scope.collectionObject, function (item) { tableRow = tableRow + ['<li>', '<div class="col-md-1">' + item.FirstName + '</div> ', '<div class="col-md-1 ">' + item.LastName + '</div> ', '<div class="col-md-1 ">' + item.State + '</div> ', '<div class="col-md-1 ">' + item.Id + '</div> ', '<div class="col-md-1 ">' + $filter('date')(item.BirthDate, 'dd-MMM-yyyy') + '</div> ', '</li>'].join(''); }); })
接下來(lái)就是將內(nèi)容渲染到表格控件中,也就是HTML<DIV>repeater-alternative
標(biāo)簽中。
首先必須理解Angular的Directive
機(jī)制,簡(jiǎn)單而言,就是我們來(lái)指示Angular,當(dāng)指定的變量被發(fā)現(xiàn),就開(kāi)始執(zhí)行后臺(tái)操作。
var userDirectives = angular.module([]); userDirectives.directive('DOMElementFound', function () { return { replace: true, link: function ($scope, $elem, attrs) { //后臺(tái)處理操作 } } });
我們會(huì)通知Angular,當(dāng)發(fā)現(xiàn)"repeater-alternative" 元素,則將以下數(shù)據(jù)渲染到列表行中。
代碼如下:
var userDirectives = angular.module([]); userDirectives.directive('repeaterAlternative', function () { return { replace : true, link: function ($scope, $elem, attrs) { $scope.$watch('collectionObject', function (oldValue, newValue) { var tableRow = ""; angular.forEach($scope.collectionObject, function (item) { tableRow = tableRow + ['<li>', '<div class="col-md-1">' + item.FirstName + '</div> ', '<div class="col-md-1 ">' + item.LastName + '</div> ', '<div class="col-md-1 ">' + item.State + '</div> ', '<div class="col-md-1 ">' + item.Id + '</div> ', '<div class="col-md-1 ">' + $filter('date')(item.BirthDate, 'dd-MMM-yyyy') + '</div> ', '</li>'].join(''); }); //If IE is your primary browser, innerHTML is recommended to increase the performance $elem.context.innerHTML = tableRow; //If IE is not your primary browser, just appending the content to the element is enough . //$elem.append(tableRow); }); } } });
總結(jié)
在本文中,主要模擬了ng-repeat的工作方式和邏輯,但只限于靜態(tài)內(nèi)容,所以輸出結(jié)果與調(diào)用ng-repeat結(jié)果相同,但是渲染更快,因?yàn)樵摲椒ㄖ挥幸环N數(shù)據(jù)綁定方式和少量的$watch。以上就是這篇文章的全部?jī)?nèi)容,希望本文的內(nèi)容能對(duì)大家的學(xué)習(xí)或者工作有所幫助,如果有疑問(wèn)大家可以留言交流。
- Angularjs通過(guò)指令監(jiān)聽(tīng)ng-repeat渲染完成后執(zhí)行腳本的方法
- AngularJs ng-repeat 嵌套如何獲取外層$index
- AngularJS 獲取ng-repeat動(dòng)態(tài)生成的ng-model值實(shí)例詳解
- AngularJS入門(mén)(用ng-repeat指令實(shí)現(xiàn)循環(huán)輸出
- Angularjs的ng-repeat中去除重復(fù)數(shù)據(jù)的方法
- AngularJS使用ng-repeat指令實(shí)現(xiàn)下拉框
- AngularJS基礎(chǔ) ng-repeat 指令簡(jiǎn)單示例
- Angularjs渲染的 using 指令的星級(jí)評(píng)分系統(tǒng)示例
- Angular ng-repeat遍歷渲染完頁(yè)面后執(zhí)行其他操作詳細(xì)介紹
- Angular.js中下拉框?qū)崿F(xiàn)渲染html的方法
- Angular將填入表單的數(shù)據(jù)渲染到表格的方法
- AngularJS監(jiān)聽(tīng)ng-repeat渲染完成的兩種方法
相關(guān)文章
AngularJS使用ui-route實(shí)現(xiàn)多層嵌套路由的示例
這篇文章主要介紹了AngularJS使用ui-route實(shí)現(xiàn)多層嵌套路由的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Angularjs實(shí)現(xiàn)下拉框聯(lián)動(dòng)的示例代碼
本篇文章主要介紹了Angularjs下拉框聯(lián)動(dòng)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08angular-ui-sortable實(shí)現(xiàn)可拖拽排序列表
這篇文章主要介紹了angular-ui-sortable實(shí)現(xiàn)可拖拽排序列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Angular實(shí)現(xiàn)svg和png圖片下載實(shí)現(xiàn)
這篇文章主要介紹了Angular實(shí)現(xiàn)svg和png圖片下載實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Angular4實(shí)現(xiàn)圖片上傳預(yù)覽路徑不安全的問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于Angular4實(shí)現(xiàn)圖片上傳預(yù)覽路徑不安全問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12Angular.js回顧ng-app和ng-model使用技巧
這篇文章主要回顧Angular.js中ng-app和ng-model使用技巧,感興趣的小伙伴們可以參考一下2016-04-04理解Angular的providers給Http添加默認(rèn)headers
本篇文章主要介紹了理解Angular的providers給Http添加默認(rèn)headers,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下2017-07-07