42

I am trying to learn angular by following the official tutorial but when following steps for hero component and hero detail component, it raises an error "RangeError: Maximum call stack size exceeded".

The hero.component.html:

<ul class="heroes">
  <li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
  </li>
</ul>

<!-- 
<app-hero-detail [hero]="selectedHero"></app-hero-detail> -->


<app-heroes></app-heroes>

The detail component:

<div *ngIf="hero">

  <h2>{{hero.name}} Details</h2>
  <div><span>id: </span>{{hero.id}}</div>
  <div>
    <label>name:
      <input [(ngModel)]="hero.name" placeholder="name"/>
    </label>
  </div>

</div>

<app-hero-detail [hero]="selectedHero"></app-hero-detail>

The hero component

import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';
import { HeroService } from '../hero.service';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
  heroes: Hero[];

  selectedHero: Hero;

  constructor(private heroService: HeroService) { }

  ngOnInit() {
    this.getHeroes();
  }

  getHeroes(): void {
    this.heroes = this.heroService.getHeroes();
  }

  onSelect(hero: Hero): void {
    this.selectedHero = hero;
  }

}

The hero.detail component

import { Component, OnInit, Input } from '@angular/core';
import { Hero } from '../hero';

@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {

  @Input() hero: Hero;
  constructor() { }

  ngOnInit() {
  }

}

one thing to mention is that when <app-heroes></app-heroes> is commented, the list page is loaded without an error

Error message screenshot any help is appreciated

4
  • 1
    In your example you render component inside yourself, so you never finish this operation and all time render another child component (if second part of block is <app-hero-detail> Commented Dec 13, 2018 at 6:56
  • If you are looking in console, there might be an error before the error that is mentioned in your question. Can you please check and update the same Commented Dec 13, 2018 at 7:17
  • @SachinGupta screenshot uploaded Commented Dec 13, 2018 at 7:20
  • Same error messages, if any feature module, are same in child and parent module. Commented Aug 15, 2022 at 18:46

14 Answers 14

47

1.This error occur when there is an infinite loop. As you have mentioned that the page loads when app-heroes is commented, this might be used as selector-name for more than one component which is not allowed. This can cause an infinite loop and fail to load components.

  1. Try making below edits,

hero.component.html

<ul class="heroes">
  <li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
  </li>
</ul>

<app-hero-detail [hero]="selectedhero"></app-hero-detail> 

hero.detail.component.html

<div *ngIf="hero">
  <h2>{{hero.name}} Details</h2>
  <div><span>id: </span>{{hero.id}}</div>
  <div>
    <label>name:
      <input [(ngModel)]="hero.name" placeholder="name"/>
    </label>
  </div>
</div>

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

hello . have checked and that doesnt seem to be the case . (question updated)
Can you please create an example to replicate your problem here stackblitz.com
@AbdulAli doesn't seem to help then why was it marked as answer?
@CaptainPrinny It worked after eidting the answer :)
9

This error occurred when I used a circular module import mistakenly on Angular 9.

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
@NgModule({
 imports: [
    CommonModule,
    ModuleTwo
  ],
 
})
export class ModuleOne {
}

and

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
    @NgModule({
    imports: [
        CommonModule,
        ModuleOne
      ],
     
    })
    export class ModuleTwo {
    }

Comments

7

In your example you render component inside yourself, so you never finish this operation and all time render another child component (if second part of block is

Update - more details:

If you write app with components, all components are hierarchical, so you can include the same component inside yourself only if you are sure, that this is limited amount of loop inside. In your code example you has unlimited nested components, because child component generate next child component inside yourself body. In result your browser display error: RangeError: Maximum call stack size exceeded

hero.component.html

<ul class="heroes">
  <li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
  </li>
</ul>

<!-- 
<app-hero-detail [hero]="selectedHero"></app-hero-detail> -->


<app-heroes></app-heroes>

app-hero-details.component.html

<div *ngIf="hero">

  <h2>{{hero.name}} Details</h2>
  <div><span>id: </span>{{hero.id}}</div>
  <div>
    <label>name:
      <input [(ngModel)]="hero.name" placeholder="name"/>
    </label>
  </div>

</div>

// you should comment line below
// <app-hero-detail [hero]="selectedHero"></app-hero-detail>

2 Comments

thank you for the answer. can you kindly explain a bit as am completely new to angular and the components' hierarchy
Great answer... A good example would be naming a component "header", which would conflict with a header tag inside the component partial.
7

I will add an answer that describes a different cause for this error when upgrading to angular 8 an existing application and using new routing features.

In my case, I added to each lazy loaded route the data object with preload set to true || false using the new syntax:

  {
    path: '',
    loadChildren: () => import('./views/home/home.module').then(mod => mod.HomeModule),
    data: { preload: true }
  },

However it took me a while to realize that I had left the preloadingStrategy set to PreloadAllModules in my RouterModule forRoot declaration:

@NgModule({
  imports: [RouterModule.forRoot(
    routes,
    {
      preloadingStrategy: PreloadAllModules, <-- This is the cause
    })],
    ...

Removing preloadingStrategy from the module forRoot declaration and relying on the route data preload definitio fixes the issue.

Comments

6

I was dealing with this error when my intellisense accidentally added the module name (instead of a specific component name) to the exports for the module:

@NgModule({
  declarations: [
    FooComponent,
    // ...
  ],
  imports: [CommonModule],
  exports: [
    FooModule, // <== should be 'FooComponent'
    // ...
  ], 
})
export class FooModule {}

It definitely would be nice if the error was a little more descriptive though, as this tool me some time

1 Comment

Mine was similar in nature. AppModule was pulled into a child module which is imported inside of AppModule. In my case, Intellij did the damage.
4

You have the <app-hero-detail> displayed inside the details HTML.

<app-hero-detail [hero]="selectedHero"></app-hero-detail>

Please try removing this. A similar line is commented in the hero.component.html, you can uncomment that.

Comments

2

A different cause for this error when upgrading to angular 8 an existing application and using new routing features in separate file.

In my case not importing the featureRouterModule to its particular featureModule.ts cause maximum stack exceed error.Since angular can't find its particular router file registered

Comments

2

I was able to reproduce this error message when I created a module and I forget to add the RoutingModule.

@NgModule({
  declarations: [
  SuccessComponent,
  CheckoutComponent
],
  imports: [
    ShareModule,
    PaymentRoutingModule // Forget to add it.
  ]
})
export class PaymentModule { }

I have no idea why I got this error, but maybe another person can make the same mistake.

Comments

2

Possible cases for this error which I found:

  • 2 or more modules with the same name
  • Wrong imports in your app.module.ts file
  • Problem with compilation

Solution that you can try:

  • for point 1, change the names and make them unique
  • for point 2, make sure all imports are intended as per your use case for a module, do check for inter-dependency between modules
  • for point 3, rare but happens if browser doesn't have enough memory to process, so try to restart your code editor, browser or your PC.

1 Comment

Thanks for this! I had a routing module export with the module export name, just needed to add "routing"to the name.
1

This happened when I was subscribing to an observable more frequently than required. I was subscribing to the below observable on every Enter key press,

this.filteredOptions.subscribe((res) => this.addSelectedContacts(event, res[keyManager.activeItemIndex]))

This was obviously not required, I had to limit the observable to only get the first emission by adding pipe(take(1)) as,

this.filteredOptions.pipe(take(1)).subscribe((res) => this.addSelectedContacts(event, res[keyManager.activeItemIndex]))

You might also use the swithMap operator syntax to dismiss previous subscriptions.

Comments

0

I named two modules similarly and didn't pay attention to which I imported. If you import a module inside itself it will cause circular import till maximum call stack is reached.

Comments

0

I solved an infinite loop ploblemma I was having after I removed a call from a service I was doing within the same service, but with different behavior.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

In my case, I was mistakenly putting some components in the import array of a module instead of putting them in the exports array.

Comments

-1

Angular 12

I encounter this on a infinite loop which causes ui to change on every iteration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.