This is my first Angular project, and I'm hoping to get some help with some simple data I'm trying to display.
I have a component (AuthorsComponent) that is referenced in my app.modules.ts file in the declarations property and imported into the file. In the modules file, I am also instantiating an AuthorsService class in the providers property to then inject into my AuthorsComponent constructor as a dependency.
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AuthorsComponent } from './authors/authors.component';
import { AuthorsService } from './authors.service';
@NgModule({
declarations: [
AppComponent,
AuthorsComponent
],
imports: [
BrowserModule,
AppRoutingModule,
],
providers: [
AuthorsService
],
bootstrap: [AppComponent]
})
export class AppModule { }
// authors.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class AuthorsService {
getAuthors() {
return ["author1", "author2", "author3"];
}
}
I use the dependency to invoke a bunk data service array and set it to the authors property in the AuthorsComponent class.
// authors.component.ts
import { Component, OnInit } from '@angular/core';
import { AuthorsService } from '../authors.service';
@Component({
selector: 'authors',
templateUrl: './authors.component.html',
styleUrls: ['./authors.component.css']
})
export class AuthorsComponent implements OnInit {
authors;
constructor(service: AuthorsService) {
this.authors = service.getAuthors();
}
ngOnInit(): void {
}
}
The 'selected' html contains a simple header and a <ul><li></li></ul>. In my <li> I use a *ngFor directive to map? the authors array of strings to the list items.
<h2>{{ authors.length }} Authors</h2>
<ul>
<li *ngFor="let author of authors">
{{ author }}
</li>
</ul>
In the app component I declare a header, which doesn't even load, and my custom component <authors>.
<h1>Angular</h1>
<authors></authors>
Really, this is probably more info than anyone needs, as I cant even get the header <h1>Angular</h1> to render from app.component.html. However, I think it shows that I've done quite a bit of sifting through docs and understanding Angular at a high level (Watch it still be a simple mistake!) Here's the app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
}
and finally, index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HelloWorld</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-root></app-root>
</body>
</html>
To recap, there is something preventing my index.html <app-root> from receiving my app.component.html. If anyone can spot what might be causing this, it would be greatly appreciated. Thank you for your time!