Angular declare variable in the model

0

the angular model that 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 that 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
        ){

        }
    }

already try 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 works. I get error page.component.ts (24,21): Supplied parameters do not match any signature of call target.

Help

New comment 24-09-2017

I already worked. If someone serves you I have

in the page.component.ts statement

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 on the html page I have

<li *ngFor = "let page of page.pages" >
    a {{ page.name }}
</li>

I leave the code in case someone happens the same A thousand thanks

    
asked by semidf des 24.09.2017 в 08:40
source

1 answer

1

To type a variable in angular with your model you can do it like this:

pages = new Array<Page>();

this.myService.getPages().subscribe((pages: Pages[]) => this.pages = pages);
    
answered by 24.09.2017 / 09:12
source