Skip to content

Instantly share code, notes, and snippets.

@wizardnet972
Created May 23, 2018 09:00
Show Gist options
  • Select an option

  • Save wizardnet972/4c6a1ab184d30d387ef5e13205f3de57 to your computer and use it in GitHub Desktop.

Select an option

Save wizardnet972/4c6a1ab184d30d387ef5e13205f3de57 to your computer and use it in GitHub Desktop.
user component
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