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 :
- I don't have anything displayed at all ...
- 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
add a comment |
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 :
- I don't have anything displayed at all ...
- 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
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
add a comment |
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 :
- I don't have anything displayed at all ...
- 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
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 :
- I don't have anything displayed at all ...
- 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
json angular6
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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