Json retrieve data with Angular 6











up vote
-1
down vote

favorite












For my first Angular 6 project, I retrieve a json from this URL : http://localhost:8080/app/categories
it looks like this :



[
{
"id": 1,
"name": "name1",
"imagepath": "/images/im1.png",
...
"childCategory": [
{
"id": 5,
"name": "name5",
"imagepath": "/images/im5.png",
"childCategory": [

]
},
{
"id": 6,
"name": "name6",
"imagepath": "",
"childCategory": [

]
}
...
}
]


In Angular I created an interface with the fields of my json



export interface ICategory {
url : string;
id: number;
name: string;
imagepath: string;
createdat: Date;
updatedat: Date;
}


And a class with its constructor that implements the interface :



import {ICategory} from "../intefaces/ICategory";

export class Category implements ICategory{
constructor(private _url: string, private _id: number, private _name: string, private _imagepath: string, private _createdat: Date, private _updatedat: Date) {
}


I also had to create a service :



import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Category} from "../../Class/Category";

@Injectable({
providedIn: 'root'
})
export class CategoriesService {
url: string = "http://localhost:8080/app/categories";

constructor(private http: HttpClient) { }

getAll(): any {
return this.http.get<Array<Category>>(this.url);
}


I also add the service to the app.module.ts



providers: [CategoriesService],


then in my component I finally inject my service :



import { Component, OnInit } from '@angular/core';
import {Category} from "../../Class/Category";
import {CategoriesService} from "../../services/http/categories.service";

@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

categories:Array<Category> = new Array<Category>();

constructor(private categoryService:CategoriesService) {
// this.categoryService.getAll()
// .subscribe(res =>
// {
// this.categories = res;
// });
this.categories = this.categoryService.getAll();
}
ngOnInit() { }
}


In my view I use a *ngFor :



My list 
<span *ngFor="let category of categories">
{{ category}}
</span>


I have 2 issues :




  1. I don't have anything displayed at all ...

  2. how could i display the categories witch childCategory is not empty and display their childCategory ?


Thanks for your help



Comment : I changed the date onto number and i add childCategory: any
Now i can see [Object Object]. I just had to change my view by adding the element i needed to display : {{ category.name }}










share|improve this question




















  • 1




    Not all of the properties are in the interface, some of the properties that are have a type (Date) that doesn't exist in JSON and the class is currently completely pointless. Also you should type getAll more carefully, then the compiler would warn you that assigning its result to this.categories doesn't make sense.
    – jonrsharpe
    Nov 18 at 9:04












  • I change dates onto numbers ... and now i retrieve [Object Object] which is better...
    – davidvera
    Nov 18 at 10:03















up vote
-1
down vote

favorite












For my first Angular 6 project, I retrieve a json from this URL : http://localhost:8080/app/categories
it looks like this :



[
{
"id": 1,
"name": "name1",
"imagepath": "/images/im1.png",
...
"childCategory": [
{
"id": 5,
"name": "name5",
"imagepath": "/images/im5.png",
"childCategory": [

]
},
{
"id": 6,
"name": "name6",
"imagepath": "",
"childCategory": [

]
}
...
}
]


In Angular I created an interface with the fields of my json



export interface ICategory {
url : string;
id: number;
name: string;
imagepath: string;
createdat: Date;
updatedat: Date;
}


And a class with its constructor that implements the interface :



import {ICategory} from "../intefaces/ICategory";

export class Category implements ICategory{
constructor(private _url: string, private _id: number, private _name: string, private _imagepath: string, private _createdat: Date, private _updatedat: Date) {
}


I also had to create a service :



import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Category} from "../../Class/Category";

@Injectable({
providedIn: 'root'
})
export class CategoriesService {
url: string = "http://localhost:8080/app/categories";

constructor(private http: HttpClient) { }

getAll(): any {
return this.http.get<Array<Category>>(this.url);
}


I also add the service to the app.module.ts



providers: [CategoriesService],


then in my component I finally inject my service :



import { Component, OnInit } from '@angular/core';
import {Category} from "../../Class/Category";
import {CategoriesService} from "../../services/http/categories.service";

@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

categories:Array<Category> = new Array<Category>();

constructor(private categoryService:CategoriesService) {
// this.categoryService.getAll()
// .subscribe(res =>
// {
// this.categories = res;
// });
this.categories = this.categoryService.getAll();
}
ngOnInit() { }
}


In my view I use a *ngFor :



My list 
<span *ngFor="let category of categories">
{{ category}}
</span>


I have 2 issues :




  1. I don't have anything displayed at all ...

  2. how could i display the categories witch childCategory is not empty and display their childCategory ?


Thanks for your help



Comment : I changed the date onto number and i add childCategory: any
Now i can see [Object Object]. I just had to change my view by adding the element i needed to display : {{ category.name }}










share|improve this question




















  • 1




    Not all of the properties are in the interface, some of the properties that are have a type (Date) that doesn't exist in JSON and the class is currently completely pointless. Also you should type getAll more carefully, then the compiler would warn you that assigning its result to this.categories doesn't make sense.
    – jonrsharpe
    Nov 18 at 9:04












  • I change dates onto numbers ... and now i retrieve [Object Object] which is better...
    – davidvera
    Nov 18 at 10:03













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











For my first Angular 6 project, I retrieve a json from this URL : http://localhost:8080/app/categories
it looks like this :



[
{
"id": 1,
"name": "name1",
"imagepath": "/images/im1.png",
...
"childCategory": [
{
"id": 5,
"name": "name5",
"imagepath": "/images/im5.png",
"childCategory": [

]
},
{
"id": 6,
"name": "name6",
"imagepath": "",
"childCategory": [

]
}
...
}
]


In Angular I created an interface with the fields of my json



export interface ICategory {
url : string;
id: number;
name: string;
imagepath: string;
createdat: Date;
updatedat: Date;
}


And a class with its constructor that implements the interface :



import {ICategory} from "../intefaces/ICategory";

export class Category implements ICategory{
constructor(private _url: string, private _id: number, private _name: string, private _imagepath: string, private _createdat: Date, private _updatedat: Date) {
}


I also had to create a service :



import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Category} from "../../Class/Category";

@Injectable({
providedIn: 'root'
})
export class CategoriesService {
url: string = "http://localhost:8080/app/categories";

constructor(private http: HttpClient) { }

getAll(): any {
return this.http.get<Array<Category>>(this.url);
}


I also add the service to the app.module.ts



providers: [CategoriesService],


then in my component I finally inject my service :



import { Component, OnInit } from '@angular/core';
import {Category} from "../../Class/Category";
import {CategoriesService} from "../../services/http/categories.service";

@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

categories:Array<Category> = new Array<Category>();

constructor(private categoryService:CategoriesService) {
// this.categoryService.getAll()
// .subscribe(res =>
// {
// this.categories = res;
// });
this.categories = this.categoryService.getAll();
}
ngOnInit() { }
}


In my view I use a *ngFor :



My list 
<span *ngFor="let category of categories">
{{ category}}
</span>


I have 2 issues :




  1. I don't have anything displayed at all ...

  2. how could i display the categories witch childCategory is not empty and display their childCategory ?


Thanks for your help



Comment : I changed the date onto number and i add childCategory: any
Now i can see [Object Object]. I just had to change my view by adding the element i needed to display : {{ category.name }}










share|improve this question















For my first Angular 6 project, I retrieve a json from this URL : http://localhost:8080/app/categories
it looks like this :



[
{
"id": 1,
"name": "name1",
"imagepath": "/images/im1.png",
...
"childCategory": [
{
"id": 5,
"name": "name5",
"imagepath": "/images/im5.png",
"childCategory": [

]
},
{
"id": 6,
"name": "name6",
"imagepath": "",
"childCategory": [

]
}
...
}
]


In Angular I created an interface with the fields of my json



export interface ICategory {
url : string;
id: number;
name: string;
imagepath: string;
createdat: Date;
updatedat: Date;
}


And a class with its constructor that implements the interface :



import {ICategory} from "../intefaces/ICategory";

export class Category implements ICategory{
constructor(private _url: string, private _id: number, private _name: string, private _imagepath: string, private _createdat: Date, private _updatedat: Date) {
}


I also had to create a service :



import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Category} from "../../Class/Category";

@Injectable({
providedIn: 'root'
})
export class CategoriesService {
url: string = "http://localhost:8080/app/categories";

constructor(private http: HttpClient) { }

getAll(): any {
return this.http.get<Array<Category>>(this.url);
}


I also add the service to the app.module.ts



providers: [CategoriesService],


then in my component I finally inject my service :



import { Component, OnInit } from '@angular/core';
import {Category} from "../../Class/Category";
import {CategoriesService} from "../../services/http/categories.service";

@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

categories:Array<Category> = new Array<Category>();

constructor(private categoryService:CategoriesService) {
// this.categoryService.getAll()
// .subscribe(res =>
// {
// this.categories = res;
// });
this.categories = this.categoryService.getAll();
}
ngOnInit() { }
}


In my view I use a *ngFor :



My list 
<span *ngFor="let category of categories">
{{ category}}
</span>


I have 2 issues :




  1. I don't have anything displayed at all ...

  2. how could i display the categories witch childCategory is not empty and display their childCategory ?


Thanks for your help



Comment : I changed the date onto number and i add childCategory: any
Now i can see [Object Object]. I just had to change my view by adding the element i needed to display : {{ category.name }}







json angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 10:11

























asked Nov 18 at 9:00









davidvera

69115




69115








  • 1




    Not all of the properties are in the interface, some of the properties that are have a type (Date) that doesn't exist in JSON and the class is currently completely pointless. Also you should type getAll more carefully, then the compiler would warn you that assigning its result to this.categories doesn't make sense.
    – jonrsharpe
    Nov 18 at 9:04












  • I change dates onto numbers ... and now i retrieve [Object Object] which is better...
    – davidvera
    Nov 18 at 10:03














  • 1




    Not all of the properties are in the interface, some of the properties that are have a type (Date) that doesn't exist in JSON and the class is currently completely pointless. Also you should type getAll more carefully, then the compiler would warn you that assigning its result to this.categories doesn't make sense.
    – jonrsharpe
    Nov 18 at 9:04












  • I change dates onto numbers ... and now i retrieve [Object Object] which is better...
    – davidvera
    Nov 18 at 10:03








1




1




Not all of the properties are in the interface, some of the properties that are have a type (Date) that doesn't exist in JSON and the class is currently completely pointless. Also you should type getAll more carefully, then the compiler would warn you that assigning its result to this.categories doesn't make sense.
– jonrsharpe
Nov 18 at 9:04






Not all of the properties are in the interface, some of the properties that are have a type (Date) that doesn't exist in JSON and the class is currently completely pointless. Also you should type getAll more carefully, then the compiler would warn you that assigning its result to this.categories doesn't make sense.
– jonrsharpe
Nov 18 at 9:04














I change dates onto numbers ... and now i retrieve [Object Object] which is better...
– davidvera
Nov 18 at 10:03




I change dates onto numbers ... and now i retrieve [Object Object] which is better...
– davidvera
Nov 18 at 10:03

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359271%2fjson-retrieve-data-with-angular-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359271%2fjson-retrieve-data-with-angular-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin