the angular model I have is
export class Page{
constructor(
public id: number,
public name: string,
public id_status: number,
public id_menu: number,
public position: number
){
}
}
and the json I am consuming is
{
"code": 200,
"status": "success",
"data": {
"id": 1,
"name": "inicio",
"id_status": 1,
"id_menu": 1,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"position": 1,
"pages": [
{
"id": 1,
"name": "inicio222",
"id_status": 2,
"id_menu": 2,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"position": 11,
"menu": {
"id": 1,
"name": "inicio",
"id_status": 1,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"position": 1
}
},
{
"id": 22,
"name": "contactenos",
"id_status": 1,
"id_menu": 2,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"position": 2,
"menu": {
"id": 2,
"name": "contactenos",
"id_status": 1,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"position": 2
}
}
]
}
}
How can I declare in the pages model?
export class Page{
constructor(
public id: number,
public name: string,
public id_status: number,
public id_menu: number,
public position: number
){
}
}
I already tried with:
public pages: Array<any>
public pages: any
public pages: Array<Pagee> (donde Pagee es el Pagee.component.ts que tiene los atributos de pages)
None of them works. I get an errorpage.component.ts (24,21): Supplied parameters do not match any signature of call target.
Aid
New comment 09-24-2017
It already worked for me. If anyone helps I have
in the page.component.ts the declaration
public titulo: string;
public page: Array<Page>;
constructor(
private _pageService: PageService,
private _route: ActivatedRoute,
private _router: Router
){
this.titulo = 'Landing';
this.page = new Array<Page>();
}
model
export class Page{
constructor(
public id: number,
public name: string,
public id_status: number,
public id_menu: number,
public position: number,
public pages: Array<any>
){
}
}
service component
getPage(){
this._route.params.forEach((params:Params)=>{
let id = params['id'];
this._pageService.getPage(id).subscribe(
response =>{
if(response.code == 200){
this.page = response.data;
console.log(response);
}else{
this._router.navigate(['/']);
}
},
error => {
console.log(<any>error);
}
);
});
}
and in the html page I have
<li *ngFor = "let page of page.pages" >
a {{ page.name }}
</li>
I leave the code in case someone else has the same problem Thank you very much
To type a variable in angular with your model you can do it like this: