-
-
Save wizardnet972/4c6a1ab184d30d387ef5e13205f3de57 to your computer and use it in GitHub Desktop.
user component
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, Input, ViewEncapsulation, ChangeDetectorRef, Output, EventEmitter } from '@angular/core'; | |
| import { HttpClient } from '@angular/common/http'; | |
| @Component({ | |
| selector: 'app-user', | |
| encapsulation: ViewEncapsulation.Native, | |
| template: ` | |
| {{user|json}} | |
| `, | |
| }) | |
| export class UserComponent implements OnInit { | |
| public user; | |
| @Input() | |
| public set userId(id) { | |
| this.update(id); | |
| } | |
| public update(id) { | |
| this.http.get(`https://jsonplaceholder.typicode.com/users/${id}`) | |
| .subscribe((res: any) => { | |
| this.user = { | |
| name: res.name | |
| }; | |
| this.cd.detectChanges(); // <--- WHY??? this is a bug. the user change and the template is not. | |
| }); | |
| } | |
| constructor(public http: HttpClient, | |
| public cd: ChangeDetectorRef) {} | |
| ngOnInit() {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment