AngularJs ng-repeat performance issues that must be noted

Source: Internet
Author: User

AngularJs ng-repeat performance issues that must be noted
Ng-repeat of AngularJs allows us to easily traverse arrays to generate Dom elements, but improper use may also cause performance problems. In the project, after loading a list using ng-repeat, If you request data again and then filter the list, the Code may write as follows: <div ng-controller = "Test"> <button ng-click = "request () "> request new data </button> <div ng-repeat =" user in users ">{{ user. name }}</div> </div> Controller code: app. controller ('test', function ($ scope) {var users = []; for (var I = 0; I <100; I ++) {users [I] = {id: I, name: "User:" + I };}$ scope. users = users; $ scope. request = function () {// load new data from the server var result = []; // directly re-assign the value to users $ scope. users = result ;}}); check the ng-repeat source code and you will find that when the ng-repeat array is replaced, it will not reuse the existing Dom elements by default, instead, delete all of them and generate a new array Dom element: // remove all the previously generated dom for (key in lastBlockMap) {if (lastBlockMap. hasOwnProperty (key) {block = lastBlockMap [key]; elementsToRemove = getBlockElements (block. clone); $ animate. leave (elementsToRemove); forEach (elementsToRemove, function (element) {element [NG_REMOVED] = true;}); block. scope. $ destroy ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.