yallam
yallam

Reputation: 1244

Angular2 - Error: The selector "app-root" did not match any elements

I am learning Angular 2, without prior experience of Angular. I was following this tutorial: https://www.barbarianmeetscoding.com/blog/2016/03/25/getting-started-with-angular-2-step-by-step-1-your-first-component/.

And after adding new component "app-people-list" and updating the same in index.html, i get below exception. What went wrong?

    Unhandled Promise rejection: The selector "app-root" did not match any elements ; Zone: <root> ; Task: Promise.then ; Value: Error: The selector "app-root" did not match any elements
    at DefaultDomRenderer2.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DefaultDomRenderer2.selectRootElement (platform-browser.es5.js:2808)
    at DebugRenderer2.webpackJsonp.../../../core/@angular/core.es5.js.DebugRenderer2.selectRootElement (core.es5.js:13710)
    at createElement (core.es5.js:9259)
    at createViewNodes (core.es5.js:12225)
    at createRootView (core.es5.js:12154)
    at callWithDebugContext (core.es5.js:13535)
    at Object.debugCreateRootView [as createRootView] (core.es5.js:12852)
    at ComponentFactory_.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactory_.create (core.es5.js:9945)
    at ComponentFactoryBoundToModule.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactoryBoundToModule.create (core.es5.js:3333)
    at ApplicationRef_.webpackJsonp.../../../core/@angular/core.es5.js.ApplicationRef_.bootstrap (core.es5.js:4822) Error: The selector "app-root" did not match any elements
    at DefaultDomRenderer2.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DefaultDomRenderer2.selectRootElement (http://localhost:4200/vendor.bundle.js:70399:19)
    at DebugRenderer2.webpackJsonp.../../../core/@angular/core.es5.js.DebugRenderer2.selectRootElement (http://localhost:4200/vendor.bundle.js:57800:49)
    at createElement (http://localhost:4200/vendor.bundle.js:53349:23)
    at createViewNodes (http://localhost:4200/vendor.bundle.js:56315:44)
    at createRootView (http://localhost:4200/vendor.bundle.js:56244:5)
    at callWithDebugContext (http://localhost:4200/vendor.bundle.js:57625:42)
    at Object.debugCreateRootView [as createRootView] (http://localhost:4200/vendor.bundle.js:56942:12)
    at ComponentFactory_.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactory_.create (http://localhost:4200/vendor.bundle.js:54035:46)
    at ComponentFactoryBoundToModule.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactoryBoundToModule.create (http://localhost:4200/vendor.bundle.js:47423:29)
    at ApplicationRef_.webpackJsonp.../../../core/@angular/core.es5.js.ApplicationRef_.bootstrap (http://localhost:4200/vendor.bundle.js:48912:57)
api.onUnhandledError @ zone.js:643
handleUnhandledRejection @ zone.js:667
_loop_1 @ zone.js:658
api.microtaskDrainDone @ zone.js:662
drainMicroTaskQueue @ zone.js:592
zone.js:645 Error: Uncaught (in promise): Error: The selector "app-root" did not match any elements
Error: The selector "app-root" did not match any elements
    at DefaultDomRenderer2.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DefaultDomRenderer2.selectRootElement (platform-browser.es5.js:2808)
    at DebugRenderer2.webpackJsonp.../../../core/@angular/core.es5.js.DebugRenderer2.selectRootElement (core.es5.js:13710)
    at createElement (core.es5.js:9259)
    at createViewNodes (core.es5.js:12225)
    at createRootView (core.es5.js:12154)
    at callWithDebugContext (core.es5.js:13535)
    at Object.debugCreateRootView [as createRootView] (core.es5.js:12852)
    at 

    ComponentFactory_.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactory_.create (core.es5.js:9945)
        at ComponentFactoryBoundToModule.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactoryBoundToModule.create (core.es5.js:3333)
        at ApplicationRef_.webpackJsonp.../../../core/@angular/core.es5.js.ApplicationRef_.bootstrap (core.es5.js:4822)
        at DefaultDomRenderer2.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DefaultDomRenderer2.selectRootElement (platform-browser.es5.js:2808)
        at DebugRenderer2.webpackJsonp.../../../core/@angular/core.es5.js.DebugRenderer2.selectRootElement (core.es5.js:13710)
        at createElement (core.es5.js:9259)
        at createViewNodes (core.es5.js:12225)
        at createRootView (core.es5.js:12154)
        at callWithDebugContext (core.es5.js:13535)
        at Object.debugCreateRootView [as createRootView] (core.es5.js:12852)
        at ComponentFactory_.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactory_.create (core.es5.js:9945)
        at ComponentFactoryBoundToModule.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactoryBoundToModule.create (core.es5.js:3333)
        at ApplicationRef_.webpackJsonp.../../../core/@angular/core.es5.js.ApplicationRef_.bootstrap (core.es5.js:4822)
        at resolvePromise (zone.js:770)
        at zone.js:696
        at zone.js:712
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)
        at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run (zone.js:141)
        at zone.js:818
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
        at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:191)
        at drainMicroTaskQueue (zone.js:584)
        at <anonymous>

All the code is exactly as per the tutorial link above. But i am still adding some details below.

index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Calui</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-people-list></app-people-list>
</body>
</html>

app.component.spec.ts:

import { TestBed, async } from '@angular/core/testing';

import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app');
  }));

  it('should render title in a h1 tag', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!!');
  }));
});

app.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
}

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { PeopleListComponent } from './people-list/people-list.component';

@NgModule({
  declarations: [ AppComponent, PeopleListComponent ],
  imports: [ BrowserModule, FormsModule, HttpModule ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

people-list.component.spec.ts:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { PeopleListComponent } from './people-list.component';

describe('PeopleListComponent', () => {
  let component: PeopleListComponent;
  let fixture: ComponentFixture<PeopleListComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ PeopleListComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(PeopleListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should be created', () => {
    expect(component).toBeTruthy();
  });
});

people-list.component.ts:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-people-list',
  template: `
    <p>
      people-list Works!
    </p>
  `,
  styleUrls: ['./people-list.component.css']
})
export class PeopleListComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

Upvotes: 41

Views: 85380

Answers (8)

Paolo-01
Paolo-01

Reputation: 31

This can happen if <script> tag is put before the <app-root> itself. Thanks for the very clear error message Angular!

Upvotes: 3

Mayank Mani Pandey
Mayank Mani Pandey

Reputation: 33

I know this is very old query. Still, check if you have missed "" in your app.component.html or default component html file.

If not present, then do add this router-outlet. Hope this will work out.

Upvotes: 0

Vamshi
Vamshi

Reputation: 9331

Angular starts with a root component. When you give

bootstrap: [ AppComponent ]

in your app.modules.ts, angular searches for first instance of app-root in your HTML and replaces that tag with angular application. So, your AppComponent selector should match to the root component in HTML .

You need to add other component code inside app.component.html , so that angular can display them . It follows tree structure where app-root is at the top .

Upvotes: 65

Abhinav Kumar
Abhinav Kumar

Reputation: 11

The best thing you can do is keep in index.html and in app.component.html and in app-routing.modile.ts, you can put your component i.e. as the default page when the application loads.It's coming error because i haven't created the component as well as have not imported it.

Upvotes: 1

Rusty Rob
Rusty Rob

Reputation: 17173

in my case, index.html was empty, but I had other files such as brand-1.index.html and brand-2.index.html etc. In angular.json there was a file-replacement on index.html. This meant I needed to include --configuration=brand-1 or build:ssr:brand-1 somewhere in the build pipeline.

Upvotes: 1

Anand Rockzz
Anand Rockzz

Reputation: 6658

In my case, I had included angular project2's dist content into angular project1's assets dir and I was trying to access it from http://localhost:4200/assets/project2/index.html. Bu Having an iframe inside project1's component.

project1 was created with ng new project1 --prefix project1
project2 was created with ng new project2 --prefix project2.

I was getting Error: The selector "project1-root" did not match any elements.

Not an elegant way to handle 2 angular projects, but I wanted something quick to test, and it didn't work. I ended up including project2 as a separate module in project1.

Guess, listening to elders (to KISS) is a good thing =)

Upvotes: 1

CodeSlave
CodeSlave

Reputation: 457

This also happens when your javascript tag is not closed

Upvotes: 4

nightfury
nightfury

Reputation: 412

one of the simple way sometimes is just to create a new project (definitely only feasible if you are working on a basic project) - there are cases where you might have missed something - this way you can save some good time and focus on learning. This was my experience wasted almost 5 hours finding the reason why this didnt work and I was able to see output by just recreating the entire thing using basic start commands and code changes again on a new boilerplate.

Upvotes: 4

Related Questions