I'm trying to bind a src
attribute of images inside an ngFor
directive that looks like this:
<div *ngFor="imageId of imageIds">
<img [attr.src]="imageSrc(imageId)" alt="{{imageId}}">
</div>
the imageSrc
method inside the component looks like this:
imageSrc(imageId: string){
var hostUrl = "http://192.168.0.101:3000/";
var imageUrl = hostUrl+"images/"+imageId;
var imgSrc = "";
this._imageService.getImage(imageId)
.subscribe(image => {
imgSrc = hostUrl + image.url
});
return imgSrc;
}
The getImage
function in the Injectable
ImageService
looks like this:
getImage(id: string) {
var _imageUrl = "http://192.168.0.101:3000/images/"+id;
return this.http.get(_imageUrl)
.map(res => <Image>res.json().data)
.catch(this.handleError)
}
The problem is, with only two imageIds
the *ngFor
directive renders the two list items as expected (but no images displayed) but the call to the data service doesn't stop, it gets the two images over and over on what appears to be an infinite loop until the application crashes. what I'm I doing wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…