Initializing a variable in a model from a angular service -


i trying image path restful call. lot of other information not need @ moment need path loop through image path on html page. below have:

    [ {   "level" : {     "id" : 1,     "description" : "sfg level 0",     "imagename" : "guru-level-0.png",     "levelnumber" : 0,     "created" : "2017-08-14t11:14:43.687+0000",     "updated" : "2017-08-14t11:14:43.687+0000"   },   "levelimagepath" : "../../assets/images/level/guru-level-0.png" }, {   "level" : {     "id" : 2,     "description" : "sfg level 1",     "imagename" : "guru-level-1.png",     "levelnumber" : 1,     "created" : "2017-08-14t11:14:43.689+0000",     "updated" : "2017-08-14t11:14:43.689+0000"   },   "levelimagepath" : "../../assets/images/level/guru-level-1.png" }, {   "level" : {     "id" : 3,     "description" : "sfg level 2",     "imagename" : "guru-level-2.png",     "levelnumber" : 2,     "created" : "2017-08-14t11:14:43.690+0000",     "updated" : "2017-08-14t11:14:43.690+0000"   },   "levelimagepath" : "../../assets/images/level/guru-level-2.png" }, {   "level" : {     "id" : 4,     "description" : "sfg level 3",     "imagename" : "guru-level-3.png",     "levelnumber" : 3,     "created" : "2017-08-14t11:14:43.692+0000",     "updated" : "2017-08-14t11:14:43.692+0000"   },   "levelimagepath" : "../../assets/images/level/guru-level-3.png" }, {   "level" : {     "id" : 5,     "description" : "sfg level 4",     "imagename" : "guru-level-4.png",     "levelnumber" : 0,     "created" : "2017-08-14t11:14:43.693+0000",     "updated" : "2017-08-14t11:14:43.693+0000"   },   "levelimagepath" : "../../assets/images/level/guru-level-4.png" }, {   "level" : {     "id" : 6,     "description" : "sfg level 5",     "imagename" : "guru-level-5.png",     "levelnumber" : 0,     "created" : "2017-08-14t11:14:43.694+0000",     "updated" : "2017-08-14t11:14:43.694+0000"   },   "levelimagepath" : "../../assets/images/level/guru-level-5.png" } ] 

my model like:

export interface levelmodel {     level: {         id: number,         description: string,         imagename: string,         levelnumber: number,         created: date,         updated: date     },     levelimagepath: string } 

service:

@injectable() export class levelsproxy {      constructor(private httputil: httputil, private appconstants: appconstants) { }      getlevels(): observable<levelmodel[]> {         return this.httputil.get(this.appconstants.end_point_levels)             .map(response => <levelmodel[]>response.json());     }  } 

now component:

@component({     selector: 'sfg-levels',     templateurl: './levels.component.html' }) export class levelscomponent implements oninit {      private levels: levelmodel[] = [];      constructor(private levelsservice: levelsproxy) { }      ngoninit() {          this.levelsservice             .getlevels()             .subscribe((data: levelmodel[]) => {                 if(data){                     this.levels = this.levelsservice.getlevels();                 }             });     } } 

component

in html:

<picture>     <img class="shield-holder" [src]="level?.levelimagepath" alt="image description"> </picture> 

error:

/levels/levels.component.ts (23,21): type 'observable' not assignable type 'levelmodel[]'. property 'length' missing in type 'observable'.

after solution provided have following issue:

enter image description here

ngoninit() {         this.levelsservice         .getlevels(         .subscribe((data: levelmodel[]) => {             if(data){                  this.levels=data.map( val => return val.levelimagepath);                console.log(this.levels);             }         }); }  <picture *ngfor="let level of levels">     <img class="shield-holder" [src]="level" alt="image description"> </picture> 

demo : https://plnkr.co/edit/jf3hvz8qg8pwzx01c13r?p=preview

(pls ignore angular version , click on friends link see result. please find relevant code in myfriends.ts file.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -