Last active
April 21, 2021 23:12
-
-
Save ScriptBytes/2e4fd9ac16677b52206b724eeb256126 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { Person } from './person.model'; | |
import { PersonService } from './person.service'; | |
@Component({ | |
selector: 'app-person', | |
template: ` | |
<div>Selected Person: <span *ngIf="selected$ | async as person">{{person.name}}</span></div> | |
<div *ngFor="let per of people$ | async" (click)="select(per)"> | |
<div>Name: {{per.name}}</div> | |
<div>Username: {{per.username}}</div> | |
</div> | |
`, | |
styles: [ | |
] | |
}) | |
export class PersonComponent implements OnInit { | |
people$ = this.personService.items$; | |
selected$ = this.personService.selected$; | |
constructor(private personService: PersonService) { } | |
ngOnInit(): void { | |
// Load the data | |
this.personService.load(); | |
// Examples of using the success observables | |
// Try to avoid subscribing directly in a component like this. Unsubscribe if you have to! | |
this.personService.updateSuccess$.subscribe((per) => alert('Update Success!')); | |
this.personService.deleteSuccess$.subscribe((per) => alert('Delete Success!')) | |
} | |
select(per: Person) { | |
// To select an item, you can call select it with select or get(id) | |
// Using get will hit the api to get the latest from the DB | |
this.personService.select(per); | |
// OR | |
// this.personService.get(per.id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment