I am trying to get a image path from a restful call. I get a lot of other information that I do not need at the moment but I do need the path so I a loop through the image path on the HTML page. Below is what I 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 look 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 for the 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();
}
});
}
}
in HTML:
<picture>
<img class="shield-holder" [src]="level?.levelImagePath" alt="image description">
</picture>
Error:
/levels/levels.component.ts (23,21): Type 'Observable' is not assignable to type 'LevelModel[]'. Property 'length' is missing in type 'Observable'.
After a solution provided I have the following issue:


ERROR Error: Uncaught (in promise): Error: No provider for LevelsProxy!When I did this:this.levelsService .getLevels() .subscribe((data: LevelModel[]) => { if(data){ //this.levels = this.levelsService.getLevels(); } });