Skip to content

Commit b8b726d

Browse files
committed
Integrated Blazor HeroEditor in Angular app, including event binding
1 parent 92a97fc commit b8b726d

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

Angular/src/app/app.component.html

-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
<blazor-counter></blazor-counter>
2-
<!--
31
<h1>{{title}}</h1>
42
<nav>
53
<a routerLink="/dashboard">Dashboard</a>
64
<a routerLink="/heroes">Heroes</a>
75
</nav>
86
<router-outlet></router-outlet>
97
<app-messages></app-messages>
10-
-->
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
<div *ngIf="hero">
1+
<div>
22
<h2>{{hero.name | uppercase}} Details</h2>
3-
<div><span>id: </span>{{hero.id}}</div>
4-
<div>
5-
<label for="hero-name">Hero name: </label>
6-
<input id="hero-name" [(ngModel)]="hero.name" placeholder="Hero name"/>
7-
</div>
3+
<blazor-heroeditor #editor [hero]="hero"></blazor-heroeditor>
84
<button type="button" (click)="goBack()">go back</button>
95
<button type="button" (click)="save()">save</button>
106
</div>

Angular/src/app/hero-detail/hero-detail.component.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
22
import { ActivatedRoute } from '@angular/router';
33
import { Location } from '@angular/common';
44

@@ -11,7 +11,8 @@ import { HeroService } from '../hero.service';
1111
styleUrls: [ './hero-detail.component.css' ]
1212
})
1313
export class HeroDetailComponent implements OnInit {
14-
hero: Hero | undefined;
14+
hero: Hero = { id: 0, name: "" };
15+
@ViewChild("editor") editor!: ElementRef;
1516

1617
constructor(
1718
private route: ActivatedRoute,
@@ -23,12 +24,22 @@ export class HeroDetailComponent implements OnInit {
2324
this.getHero();
2425
}
2526

27+
ngAfterViewInit(): void {
28+
// Register web component event (we need to bind "this" to the Angular component!)
29+
this.editor.nativeElement.heroChanged = this.onHeroChanged.bind(this);
30+
}
31+
2632
getHero(): void {
2733
const id = parseInt(this.route.snapshot.paramMap.get('id')!, 10);
2834
this.heroService.getHero(id)
2935
.subscribe(hero => this.hero = hero);
3036
}
3137

38+
onHeroChanged(hero: Hero) {
39+
console.log("Hero changed from Blazor component: " + hero.name);
40+
this.hero = hero;
41+
}
42+
3243
goBack(): void {
3344
this.location.back();
3445
}

Angular/src/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
</head>
1212
<body>
1313
<app-root></app-root>
14+
<div id="app"></div> <!-- Unused. Just to make Blazor happy. -->
1415
</body>
1516
</html>

0 commit comments

Comments
 (0)