Skip to content

Commit 1fd93d0

Browse files
committed
Componentizing hero-detail
1 parent 418d524 commit 1fd93d0

7 files changed

Lines changed: 53 additions & 11 deletions

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { BrowserModule } from '@angular/platform-browser';
44

55
import { AppComponent } from './app.component';
66
import { HeroesComponent } from './heroes/heroes.component';
7+
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
78

89
@NgModule({
910
declarations: [
1011
AppComponent,
11-
HeroesComponent
12+
HeroesComponent,
13+
HeroDetailComponent
1214
],
1315
imports: [
1416
BrowserModule,

src/app/hero-detail/hero-detail.component.css

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div *ngIf="hero">
2+
<h2>{{ hero.name | uppercase}} Details</h2>
3+
<div>id: {{ hero.id }}</div>
4+
<div>
5+
<label for="hero-name">Hero name: </label>
6+
<input id="hero-name" [(ngModel)]="hero.name" placeholder="name">
7+
</div>
8+
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { HeroDetailComponent } from './hero-detail.component';
4+
5+
describe('HeroDetailComponent', () => {
6+
let component: HeroDetailComponent;
7+
let fixture: ComponentFixture<HeroDetailComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [ HeroDetailComponent ]
12+
})
13+
.compileComponents();
14+
});
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(HeroDetailComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Component, Input, OnInit } from '@angular/core';
2+
import { Hero } from '../heroes/hero';
3+
4+
@Component({
5+
selector: 'app-hero-detail',
6+
templateUrl: './hero-detail.component.html',
7+
styleUrls: ['./hero-detail.component.css']
8+
})
9+
export class HeroDetailComponent implements OnInit {
10+
@Input() hero?: Hero
11+
12+
constructor() { }
13+
14+
ngOnInit(): void {
15+
}
16+
}

src/app/heroes/heroes.component.html

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,4 @@ <h2>My Heroes</h2>
77
</button>
88
</li>
99
</ul>
10-
11-
<div *ngIf="selectedHero">
12-
<h2>{{ selectedHero.name | uppercase}} Details</h2>
13-
<div>id: {{ selectedHero.id }}</div>
14-
<div>
15-
<label for="hero-name">Hero name: </label>
16-
<input id="hero-name" [(ngModel)]="selectedHero.name" placeholder="name">
17-
</div>
18-
</div>
10+
<app-hero-detail [hero]="selectedHero"></app-hero-detail>

src/app/heroes/heroes.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ export class HeroesComponent implements OnInit {
1919
onSelect(hero: Hero): void {
2020
this.selectedHero = hero
2121
}
22-
2322
}

0 commit comments

Comments
 (0)