Displaying a list in Angular 2/4 without keeping track of any modifications

Is there a way to display a list of strings without tracking changes? (I'm experiencing performance issues).

Fetching a list of languages from a REST API and then dynamically generating dropdowns for each language using *ngFor is causing the app to slow down when there are 10+ dropdowns (10 dropdowns * 200 languages = 2000 items to track).

The ideal solution would be to disable change tracking on the languages list since it will never be altered, but this process is not documented.

Code

Controller

langs: string[] = ['English', 'German', 'Spanish', ...];

View

<select ...>
    <option *ngFor="lang of langs">{{lang}}</option>
</select>

Answer №1

To assist Angular in monitoring the addition or removal of items, we can implement a custom trackBy function. This function is responsible for returning a unique identifier for each item based on its index and current value.

With this custom tracking mechanism in place, Angular can efficiently detect which items have been added or removed when the collection changes. It then only creates or destroys the necessary elements accordingly. For more information, refer to the documentation available at: https://angular.io/api/common/NgForOf

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is preventing me from returning the result of $.ajax, but I can return the result of $http.post?

I am facing an issue with having 2 return statements in my code: return $http.post({ url: CHEAPWATCHER.config.domain + 'api/Authenticate', contentType: 'application/x-www-form-urlencoded; charset=UTF-8', data: data }); re ...

Despite a successful request, the React Redux action was not dispatched

I'm currently working on adding authentication to my project. While the user registration part seems to be working, the actions are not being dispatched. Strangely, a similar action for fetching data is working fine and dispatching the actions. Here&a ...

Troubleshooting in WebStorm: Uncovering the Root Cause Within an npm Package (node:36378) [DEP0005] - Warning: Deprecation Warning

Over the past 6 months, I've been encountering an error that seems to have surfaced after an update to node.js. (node:36378) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), ...

Issue with IE11: Main.js unable to load app.module.js as ./app.module

My angular2 project is currently functioning smoothly in the latest versions of Chrome and Firefox. I am using gulp for my build automation process and systemjs for module loading. However, when I try to access the application in IE11, it fails to load a ...

Incorporating JavaScript code within a partial response retrieved via AJAX

Is it frowned upon to include JavaScript within partials fetched via AJAX? Let's imagine a scenario where I have a button on that retrieves a form using AJAX. It is also desired to implement some jQuery event handlers for this form (such as validati ...

React - callbackFromApp function is executing only one time when clicked

Whenever I click a button within my child React module, it is meant to increment the timer and then pass back the timer in minutes and total seconds to the parent component where it will be stored as state. The issue I am facing is that when I click the b ...

Is it possible to save non-performance affecting data in MongoDB without impacting query speed?

Our application has a unique requirement to save data in schema less documents for querying and sorting purposes. MongoDB has proven to be a reliable solution that meets our needs effectively. In each document, there is a portion of data used for display ...

Firefox smoothly shifts when a class is included

Take a look at If you click on one of the large brown rectangles, a jQuery script adds a "dropped" class which changes the top position. Surprisingly, this transition effect does not work in the latest version of Firefox, unlike in other browsers. I am pu ...

Avoiding reloading of iframe when switching between components in ReactJS

I am facing an issue with my main component that contains two child components. These child components have an array of iframe and we switch between them based on certain conditions. The problem arises when the iframe's reload every time we move betwe ...

Retrieve webpage content using an ajax request in JavaScript

I'm working on an HTML page with an Ajax call to load table content. <html> .... <script sec:haspermission="VIEW_NOTE" th:inline='javascript'> require(['app/agent/viewGlobalAgent'], function (){ ...

jQuery.ajax encountering failure to read success function

Hey everyone, I need some assistance with web programming. I'm facing an issue with ajax while trying to read a JSON response. The catch is, I have to use Internet Explorer for this task, so switching browsers is not an option. I'm stuck because ...

What could be causing my JavaScript source to fail to load in an HTML document?

Currently, I am in the process of creating a basic offline dinosaur game using HTML, JS, and CSS that is well-known to everyone. I have been diligently working on it and have successfully made everything function for a few hours. However, out of nowhere, m ...

loading a dynamic grid using Ajax with jQuery

Setting up a jquery grid on a standard webpage gridPage.html involves incorporating HTML and JavaScript code as follows: $("#gridtable").jqGrid('setGridParam', { data: [ {'type': 'aType', ...

Encountered difficulty in setting cookie with ngx-cookie-service

I'm currently using angular 9 and I have an issue with setting a cookie. I have integrated ngx-cookie-service 3.0.4 into my project and I'm attempting to set the cookie as shown below: this.cookieService.set("cookieName", user.tokenId, date, "/" ...

Utilize the power of Ionic by seamlessly toggling a function with the ion

I'm using ion-toggle to switch my app between dark mode and light mode using two functions, enableDark() and disableDark(). Although the code seems fine, I want the entire app to turn into dark mode when the ion-toggle is checked, and revert to light ...

I am currently analyzing a JSON file that contains deeply nested JavaScript Objects. My goal is to rearrange the data so that objects containing a specific field value are placed at the top of the list

Currently, I am parsing a JSON file which contains a map of JavaScript objects. For instance: { offers : { "1":{"id":"1", "category":"a", "offerType":"LS"}, "2":{"id":"2", "category":"a", "offerType":"EX"}, ...

Parcel React component library throws an error stating that "require is not defined

Error message: Uncaught ReferenceError: require is not defined at index.js?5WdmUIncGTkIrWhONvlEDQ:1:1 (anonymous) @ index.js?5WdmUIncGTkIrWhONvlEDQ:1 First lines of index.js: require("./index.css"); var $cI6W1$lodash = require("lodash&q ...

Obtain the predecessor of the element that was clicked

I have a complex structure with nested tables, rows, and cells, each containing div elements with multiple spans. I am trying to create a function that will capture the clicked cell every time I click on either the cell itself or any of its child elements ...

Tips for ensuring a file has downloaded correctly

For my current project, I have a requirement to download a file which should be automatically deleted after being successfully downloaded. To ensure that the file is completely downloaded before proceeding with deletion, I initially set async:false in the ...

Angular not triggering event upon changing URL fragment subscription

I'm currently using Angular 13 and attempting to subscribe to the ActivatedRoute 'fragment'. However, I am facing an issue where the subscription only receives a notification when the page initially loads, and does not update when the fragme ...