Skip to content

Commit

Permalink
Integrated Blazor HeroEditor in Angular app, including event binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenoage committed Nov 8, 2023
1 parent 92a97fc commit b8b726d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
3 changes: 0 additions & 3 deletions Angular/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<blazor-counter></blazor-counter>
<!--
<h1>{{title}}</h1>
<nav>
<a routerLink="/dashboard">Dashboard</a>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
-->
8 changes: 2 additions & 6 deletions Angular/src/app/hero-detail/hero-detail.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<div *ngIf="hero">
<div>
<h2>{{hero.name | uppercase}} Details</h2>
<div><span>id: </span>{{hero.id}}</div>
<div>
<label for="hero-name">Hero name: </label>
<input id="hero-name" [(ngModel)]="hero.name" placeholder="Hero name"/>
</div>
<blazor-heroeditor #editor [hero]="hero"></blazor-heroeditor>
<button type="button" (click)="goBack()">go back</button>
<button type="button" (click)="save()">save</button>
</div>
15 changes: 13 additions & 2 deletions Angular/src/app/hero-detail/hero-detail.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';

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

constructor(
private route: ActivatedRoute,
Expand All @@ -23,12 +24,22 @@ export class HeroDetailComponent implements OnInit {
this.getHero();
}

ngAfterViewInit(): void {
// Register web component event (we need to bind "this" to the Angular component!)
this.editor.nativeElement.heroChanged = this.onHeroChanged.bind(this);
}

getHero(): void {
const id = parseInt(this.route.snapshot.paramMap.get('id')!, 10);
this.heroService.getHero(id)
.subscribe(hero => this.hero = hero);
}

onHeroChanged(hero: Hero) {
console.log("Hero changed from Blazor component: " + hero.name);
this.hero = hero;
}

goBack(): void {
this.location.back();
}
Expand Down
1 change: 1 addition & 0 deletions Angular/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
</head>
<body>
<app-root></app-root>
<div id="app"></div> <!-- Unused. Just to make Blazor happy. -->
</body>
</html>

0 comments on commit b8b726d

Please sign in to comment.