Search code examples
angularangular6

Why my file app.component.html is not working?


My basic angular app is not displaying the app.component.html content, always I have the content of index html.

Index HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Angular 6</title>
    <base href="/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>

    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>

    <script src="systemjs.config.js"></script>
    <script>
      System.import('main.js').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
      <my-app></my-app>
  </body>
</html>

App Component ts:

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

@Component({
  selector: 'my-app',
    templateUrl: 'wwwroot/app.component.html'
})
export class AppComponent { title = 'My First Angular App!'; }

App Module ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

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

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

app component html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
   abc
    
</body>
</html>

Result:

enter image description here

enter image description here

I'm using angular 6 with an ASP.NET CORE 1.1 application.


Solution

  • app.component.html isn't the root of your web page, it's the root of your angular app. index.html is the only place where your <html> tag should be. index.html contains the <my-app></my-app> tag that is the root of your angular app.

    If you put <html></html> in your angular.component.html, when your angular app is put into index.html after running 'ng start' your compiled web page will look something like this:

    <html> <!-- from index.html -->
       ...
       <my-app>
          <html> <!-- from app.component.html -->
             ...
          </html>
       </my-app>
    </html>

    and your webpage can't have two <html> root elements in it.

    To get your example working take the following tags out of app.component.html:
    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title> and <body>