Struggling with the few available Angular 5 Typescript documentation on the new ways of how to implement CRUD methods using Observable with HttpClient, I came up with the next code, but still stuck to show data from a web API:
Here is my model:
export interface Value {
id: number;
name: string;
}
My service:
export class ValueService {
baseUrl = environment.apiUrl;
constructor(private authHttp: HttpClient) {}
getValues() {
return this.authHttp
.get<Value[]>(this.baseUrl + 'values', { observe: 'response'});
// here my baseUrl is 'http://example.com/api/'
}
}
My component:
@Component({
selector: 'app-values',
templateUrl: './values.component.html',
styleUrls: ['./values.component.css']
})
export class ValuesComponent implements OnInit {
values: Value[];
constructor(private valueService: ValueService, private route: ActivatedRoute) { }
ngOnInit() {
this.route.data.subscribe(data => {
this.values = data['values'].result;
});
}
}
And finally, my template:
<ul>
<li>
<div *ngFor="let value of values | async"> {{value.name}}</div>
</li>
</ul>
My data is shown in the browser console with HTTP 200 result, but not on my page, What am I missing to get it rendered on my page?