in Education by
It seems using getters inside templates causes Angular change detection to go into a loop (getter gets called hundreds of times). After reading up a ton on similar issues out there I cannot seem to get a clear answer. Background info: I do believe using getters inside the template is the cleanest approach from a maintainability point of view, however seemingly because Angular cannot know if the getter value changed until it calls it, it just calls it all the time. I so far found three alternatives: Stop using getters, make all properties public and access them directly Store the value of each getter into a public property on the component and bind that instead of the getter in the template Change the changeDetection mode of Angular from default to onPush Option 1 would seem counterintuitive to the benefit of using Typescript classes. Option 2 would seem like unnecessary code duplication and reduce maintainability, option 3 would require a significant refactoring. Here is an example (simplified for illustrative purpose) Model: export class UserModel { private id: string; get getId() { console.log('Getting id'); return this.id; } set setId(id) { this.id = id; } constructor() { } } Component.html

Test

{{user.getId}}

Component.ts import {Component, OnInit} from '@angular/core'; import {TestModel} from './test.model'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { public user: UserModel; ngOnDestroy() { if (this.userObserver) { this.userObserver.unsubscribe(); } } ngOnInit() { this.userObserver = this.userObservable.subscribe( (data: UserModel) => { this.user = data; }, (err: any) => { console.error(err); }); } } Will output the following console log: console.log output Can anyone recommend the best practice to avoid unnecessary loops while working with complex models in Angular? Or even a proper way to debug this type of behavior, as it stands now I am console.logging the getter methods and watching for memmory usage spikes. EDIT (Answer) After more time investigating, digging through stack traces I found out the infinite change detection loop was actually being caused by a service we inject called 'Sentry'. Apparently it causes an issue when console.log is used that triggers change detection. Found a github issue about it here: github.com/getsentry/sentry-javascript/issues/1883 Did not find a solution (seems inherently incompatible), will update if I find a fix for it. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Use ChangeDetectionStrategy.onPush Run the following command to make this the default for your project when creating new components via the CLI. ng config schematics.@schematics/angular.component.changeDetection OnPush Generally speaking try to avoid complex getters or calling functions from within a template. If you need to transform data consider using Pipes, which are memoized.

Related questions

0 votes
    Here is a pure Python-specific design question: class MyClass(object): ... def get_my_attr(self): ... def ... you use and why? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I can't solve this for almost four hours, and i can't find any helpful documentation for this ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    How can looping be done over a list of hosts in a group, inside of a template?...
asked Jul 29, 2021 in Technology by JackTerrance
0 votes
    I was wondering if there is a way to compress my code using a loop, this is my code : ( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I was wondering if there is a way to compress my code using a loop, this is my code : ( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have this template that I used everywhere in my project : {{ event.title }} {{ event.content } ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I have this template that I used everywhere in my project : {{ event.title }} {{ event.content } ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    To fit a graphic or clip art on to a slide you can change the (a) Design template (b) Slide layout (c) Colour scheme (d) Outline Select the correct answer from above options...
asked Dec 11, 2021 in Education by JackTerrance
0 votes
    I have loop inside loop like this: var len = bolumlerUnique.length; function bolumleriGonder() { for (i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I have loop inside loop like this: var len = bolumlerUnique.length; function bolumleriGonder() { for (i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I have loop inside loop like this: var len = bolumlerUnique.length; function bolumleriGonder() { for (i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    I am trying to declare a list which can either be full of type 'a' data or type 'b' at a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I am trying to declare a list which can either be full of type 'a' data or type 'b' at a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    In the following program, if I set the variable $foo to the value 1 inside the first if statement, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have this array of objects. let links = [ { url: 'some url 1', status: 200 }, { url: ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
...