Javascript 请求二维码并使用Angular JS显示图像
我的计划是使用API(如Google Charts API)创建二维码,然后在我的网站上显示生成的二维码。我创建了一个函数,可以从Google Chart API请求二维码图像:Javascript 请求二维码并使用Angular JS显示图像,javascript,angularjs,ajax,google-api,qr-code,Javascript,Angularjs,Ajax,Google Api,Qr Code,我的计划是使用API(如Google Charts API)创建二维码,然后在我的网站上显示生成的二维码。我创建了一个函数,可以从Google Chart API请求二维码图像: .controller('DealCtrl', function($scope, $http) { $http.get("https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=160%20FSU728F29G&coe=UTF-8"
.controller('DealCtrl', function($scope, $http) {
$http.get("https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=160%20FSU728F29G&coe=UTF-8")
.success(function(data, status, headers, config) {
$scope.qrcode = data;
}).error(function(data, status, headers, config) {
$scope.qrcode = status;
});
})
当我尝试将响应绑定为HTML时,我得到以下结果:
<div ng-bind-html="qrcode"></div>
响应似乎是一个图像(PNG)。如何在我的网站上显示此内容 返回的数据似乎是HTML。因此,您需要使用
ng bind HTML
指令将Google HTML注入页面:
返回的数据似乎是HTML。因此,您需要使用ng bind HTML
指令将Google HTML注入页面:
下面是一个使用ng绑定html的示例
但是如果没有一个更深入的例子来说明您试图实现的目标,我觉得这只是一个偶然的机会。下面是一个使用ng bind html的例子
但是,如果没有更深入的示例来说明您试图实现的目标,我觉得这只是一个不确定的结果。如果您出于某种原因不想使用图像url,并且希望从内容创建图像,您可以使用以下方法:
$http.get(
"https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=160%20FSU728F29G&coe=UTF-8",
{responseType: 'blob'})
.then(function(response) {
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL( response.data );
$scope.qrcode = '<img src="'+imageUrl+'"/>';
});
$http.get(
"https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=160%20FSU728F29G&coe=UTF-8",
{responseType:'blob'})
.然后(功能(响应){
var urlCreator=window.URL | | window.webkitURL;
var imageUrl=urlCreator.createObjectURL(response.data);
$scope.qrcode='';
});
展示
如果出于某种原因不想按原样使用图像url,并且希望使用以下内容创建图像,则应归功于:
$http.get(
"https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=160%20FSU728F29G&coe=UTF-8",
{responseType: 'blob'})
.then(function(response) {
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL( response.data );
$scope.qrcode = '<img src="'+imageUrl+'"/>';
});
$http.get(
"https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=160%20FSU728F29G&coe=UTF-8",
{responseType:'blob'})
.然后(功能(响应){
var urlCreator=window.URL | | window.webkitURL;
var imageUrl=urlCreator.createObjectURL(response.data);
$scope.qrcode='';
});
展示
归功于您可以使用此指令,然后在html代码中使用它
angular.module('av.qr', [])
.directive('avQr', function() {
return {
restrict: 'E',
template: '',
scope: {
image: '='
},
link: function(scope, elem, attr) {
var qrImage = angular.element(scope.image);
elem.append(qrImage);
}
}
})
angular.module('my.module',['av.qr'])
QR
图像附加到$scope
上,如下所示:
.controller('DealCtrl', function($scope, $http) {
$http.get("https://chart.googleapis.com/chart? cht=qr&chs=300x300&chl=160%20FSU728F29G&coe=UTF-8")
.success(function(data, status, headers, config) {
$scope.qrcode = data;
}).error(function(data, status, headers, config) {
$scope.qrcode = status;
});
})
HTML
代码中,使用如下指令:
<av-qr image="qrcode"></av-qr>
**您可以将
qrcode
图像传递给指令。您可以使用此指令
,然后在html代码中使用它
angular.module('av.qr', [])
.directive('avQr', function() {
return {
restrict: 'E',
template: '',
scope: {
image: '='
},
link: function(scope, elem, attr) {
var qrImage = angular.element(scope.image);
elem.append(qrImage);
}
}
})
angular.module('my.module',['av.qr'])
QR
图像附加到$scope
上,如下所示:
.controller('DealCtrl', function($scope, $http) {
$http.get("https://chart.googleapis.com/chart? cht=qr&chs=300x300&chl=160%20FSU728F29G&coe=UTF-8")
.success(function(data, status, headers, config) {
$scope.qrcode = data;
}).error(function(data, status, headers, config) {
$scope.qrcode = status;
});
})
HTML
代码中,使用如下指令:
<av-qr image="qrcode"></av-qr>
**您将
qrcode
图像传递给指令。数据是PNG文件,据我所知不是HTML。所以这不起作用。你能得到base64图像吗?如果是,那么你只需要写“谢谢奥利弗,我以前试过这个。”。看起来Google的响应是一个二进制字符串,而不是base64字符串,所以这也不起作用将二进制文件编码为base64。数据是PNG文件,据我所知不是HTML。所以这不起作用。你能得到base64图像吗?如果是,那么你只需要写“谢谢奥利弗,我以前试过这个。”。看起来Google的响应是一个二进制字符串,而不是base64字符串,所以这也不起作用将二进制编码为base64。我觉得还有更多的内容,我不知道你为什么要发布以获取图表。不知道你的评论是什么意思。你为什么发布以获取二维码?返回的图像是二进制数据与base64。也许是天真的问题,但url就是图像,为什么不按原样显示呢?我觉得还有更多的问题,我不知道你为什么发布来获取图表。不知道你的评论是什么意思。你为什么发布来获取二维码?返回的图像是二进制数据与Base64。也许是个天真的问题,但url就是图像,为什么不按原样显示呢?非常感谢。很高兴有一个解决方案。然而,由于这可以通过按原样显示图像来非常简单地解决,因此我将同时使用此解决方案!非常感谢。非常感谢你。很高兴有一个解决方案。然而,由于这可以通过按原样显示图像来非常简单地解决,因此我将同时使用此解决方案!非常感谢。