【发布时间】:2016-11-07 16:47:32
【问题描述】:
我正在尝试将来自服务器的图像显示为 base64 字符串。 http 和服务器详细信息对我的问题并不重要(基本上,它有效)。 我有这个不起作用的代码;即我看到组件中的所有数据,但屏幕上没有显示图像。
服务:
import { Injectable } from 'angular2/core'
import {Observable} from 'rxjs/Observable';
@Injectable()
export class ImageService {
constructor() {
}
public getImage(): Observable<string> {
return Observable.create(imageData);
}
}
const imageData: string = "iVBORw0KGgoAAAANSUhE...";
组件:
import { Component } from 'angular2/core';
import { ImageService } from './service'
@Component({
selector: 'my-app',
template: '<div><img [src]="data:image/PNG;base64,{{imageContent}}"> </div>',
providers: [ImageService]
})
export class AppComponent {
private imageContent: string = "";
constructor(imageService: ImageService) {
imageService.getImage().subscribe(response => {
this.imageContent = response;
});
}
}
如前所述,代码不起作用。我收到的不是屏幕上的图像,而是:报价不支持评估!
我会感谢这个简单问题的工作示例。
【问题讨论】:
-
如果您使用方括号绑定,那么您将绑定到一个对象并需要像这样重写它:
[src]="'data:image/PNG;base64,' + imageContent",但这将被 Angular 2 删除 - 请参阅这篇文章:@987654321 @.