I have to make a project with angular 4.
I have a web application that return me a json file like this :
{
"Microsoft ": {
"name1": [
"link1",
"link2",
"link3"
],
"name2": [
"link1",
"link2"
],
"name3": [],
"name4": [],
"name5": [
"link1"
]
}
}
And I would like to display this file with angular but I have 2 problems,
I don't know how to manipulate json with angular and I don't know why the MicrosoftResult is empty?
This is my component :
import { Component, OnInit } from '@angular/core';
import {MicrosoftService} from './microsoft.service';
import {MicrosoftResult} from './microsoft-result';
import {Observable} from 'rxjs/Observable';
import {Photos} from './photos';
@Component({
selector: 'app-microsoft',
templateUrl: './microsoft.component.html',
styleUrls: ['./microsoft.component.css']
})
export class MicrosoftComponent {
constructor(private microsoftService: MicrosoftService) {
}
microsoftResult: MicrosoftResult;
getJson(): void {
this.microsoftService.getJson()
.subscribe(
data => this.microsoftResult = data,
error => console.log('Error :: ' + error)
);
}
}
And my service :
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { MicrosoftResult } from './microsoft-result';
import {catchError} from 'rxjs/operators';
import {Observable} from 'rxjs/Observable';
import {of} from 'rxjs/observable/of';
import 'rxjs/add/operator/map';
@Injectable()
export class MicrosoftService {
apiUrl = 'http://localhost:8080/test/';
constructor(private http: HttpClient) { }
getJson(): Observable<any> {
return this.http
.get(this.apiUrl)
.map((res: any) => res.json());
}
}
And this is the microsoft result interface :
export interface MicrosoftResult {
Microsoft: JSON;
}
Then this is the html website :
<label>Utilisation d'un get : </label>
<button (click)="getJson()">Get</button>
<p>{{microsoftResult.Microsoft}}</p>
But the problem is when I open the page on the browser I have an error on the console when i open the website and when i click on the button :
ERROR TypeError: Cannot read property 'Microsoft' of undefined
at Object.eval [as updateRenderer] (MicrosoftComponent.html:14)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14693)
at checkAndUpdateView (core.js:13807)
at callViewAction (core.js:14153)
at execComponentViewsAction (core.js:14085)
at checkAndUpdateView (core.js:13808)
at callViewAction (core.js:14153)
at execComponentViewsAction (core.js:14085)
at checkAndUpdateView (core.js:13808)
at callWithDebugContext (core.js:15056)