javascript AngularJS - img ng-src 到 base64 数据(不是 url)不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27637449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS - img ng-src to base64 data (not url) not working
提问by cheziHoyzer
I'm using openfb-angular(Facebook API library) to get me/picture.
The return data is "url" contains Base64 data here is the facebook documentation.
我正在使用openfb-angular(Facebook API 库)来获取me/picture。
返回数据是“url”,包含 Base64 数据,这里是 facebook 文档。
Here is my code:
这是我的代码:
JS
JS
OpenFB.get('/me/picture', {format: 'json'}).success(function (imgData)
{
$scope.main.user.imageData = imgData;
});
HTML
HTML
<img ng-src="data:image/jpg;base64,{{main.user.imageData}}">
It's not working and I get an empty img
tag.
它不起作用,我得到一个空img
标签。
Where is my mistake?
我的错误在哪里?
回答by Manuel Eguiluz
Use ng-source directive like this:
像这样使用 ng-source 指令:
<img ng-src="{{'data:image/png;base64,'+main.user.imageData}}" >
Hope this helps.
希望这可以帮助。
回答by Bipin Bhandari
Use data-ng-src
directive like this <img data-ng-src="{{data.image_url}}">
.
使用这样的data-ng-src
指令<img data-ng-src="{{data.image_url}}">
。
In your controller set base64 string as this:
$scope.data.image_url=<your base64 image source>
在您的控制器中将 base64 字符串设置为:
$scope.data.image_url=<your base64 image source>
Hope this helps!
希望这可以帮助!
回答by Srikanth
<img data-ng-src="data:image/png;base64,{{main.user.imageData}}"/>
Hope this helps.
希望这可以帮助。
回答by kbpontius
In Angular 7, this would be:
在 Angular 7 中,这将是:
<img [src]="'data:image/png;base64,' + main.user.imageData" />
<img [src]="'data:image/png;base64,' + main.user.imageData" />