Everywhere you look you see subscribe here, subscription there, unsubscribe here, unsubscribe there
. I ask, what do we NEED all this boilerplate code for, if its possible to use a service properties in components directly.
@injectable()
export class MyService {
public logItems: Log[] = [];
getLatestLogItems() {
return this.httpService.GetLatestLogItems() // some http service
.subscribe(
(items) => {
this.logItems = items;
},
...
);
}
}
and then in component:
@Component({
selector: 'app-logs',
template: `<ul>
<li *ngFor="let log of myService.logItems"> {{ log | json}} </li>
</ul> `
})
export class MyComponent implements OnInit {
constructor(public myService: MyService) { }
ngOnInit() {
this.myService.getLatestLogItems();
}
if you want to refresh the data, simply invoke getLatestLogItems()
again.
what am I missing here ? why is Subjects required at all?