Is good practice to store data in service angular 2

Viewed 6758

In a lot of tutorials, some data (e.g. list of todos) is stored in service. Is it good practice? I'm new in Angular 2 and I want to learn how code right.

Some code example:

@Injectable()
export class JsonRestService {

    jsonObject = {};
    jsonContent = {};

    constructor(private http:Http) {
        this.getXmlInJson()
        .subscribe(
            data => this.startGeneratingHtmlTemplateProcess(data),
            error => console.log(error + " - Błąd pobrania jsona")
        );
    }

    getXmlInJson() {
        let url = "http://localhost:8080/xml";
        return this.http.get(url);
    }

    public convertRestJsonToObject$() {
        this.getXmlInJson()
        .subscribe(
            data =>  this.startGeneratingHtmlTemplateProcess(data.json),
            error => console.log(error + " - Błąd pobrania jsona")
        );
   }

   private startGeneratingHtmlTemplateProcess(data) {
        this.jsonObject = JSON.parse(JSON.parse(JSON.stringify(data))._body);
        this.jsonContent = JSON.parse(JSON.stringify(data._body));
        console.log(this.jsonObject);
        console.log("Json:");
        console.log(this.jsonContent);
   }

   showObjectInConsole() {
       console.log("Wyzwolone prze przycisk: ");
       console.log(this.jsonObject);
   }

}
2 Answers
Related