angular - Ionic2 array access in template -


i'm trying show data in template coming json in controller.

all alternatives found refers using ngfor, don't want iterate items, because want show 1 of them.

getservicio(servicioid: number) {         let getservicio = 'services?id=' + servicioid;         this.http.getrequest(getservicio) //returns json             .then(             res => {                 this.services = res[0];             }             )             .catch(             error => console.log(error)             );     } 

and in template write:

{{services.id}} 

it gives me error:

cannot read property 'id' of undefined

isn't there alternative it? or in way, passing data template , getting array index like:

{{services[0].id}} 

thanks in advance!

the problem trying display data (services.id) before arrives (http requests asynchronous) - means services.id undefined @ time template rendered. simplest solution use safe navigation operator (?) "protect" template until data arrive:

{{services?.id}} 

you can use ngif directive part of template want display services not rendered until services defined:

<div *ngif="services">     {{services?.id}} </div> 

read more safe navigation operator (?) here , more ngif directive here.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -