Angular JS 生成动态二维码
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!
“小步快跑,试错迭代” - 《腾讯传》
一、场景
二维码的场景,很多。这里是二维码一种小场景,比如分享一个链接,商品链接,项目链接,优惠券链接...
技术实现,如果用后端实现,需要构造输出一个图片流。或者后端生产二维码图片,给图片地址就好了。弊端,这个二维码就是一个链接,后端的文件 IO 操作,还得考虑存储。太费力。
如果前端实现,这样就很轻松了。这只是个分享二维码,分享出去给人家扫一扫。利用前端的 canvas,这里坐下调研。
jq 封装的 qrcode.js ,文章网上一大堆。
angular js :https://github.com/monospaced/angular-qrcode
二、使用
1.安装 angular-qrcode
git clone https://github.com/monospaced/angular-qrcode.git cd angular-qrcode npm install
2.引入 js 文件
<script src="/node_modules/qrcode-generator/js/qrcode.js"></script> <script src="/node_modules/qrcode-generator/js/qrcode_UTF8.js"></script> <script src="/node_modules/angular-qrcode/angular-qrcode.js"></script>
并在你 angular 配置中加入对这个模块的依赖:
angular .module('your-module', [ 'monospaced.qrcode', ]);
3.使用
在线案例:monospaced.github.io/angular-qrcode
使用元素:
<qrcode data="string"></qrcode>
具体配置参数:
<qrcode data="string" version="2" error-correction-level="Q" size="200" color="#fff" ba kground="#000"></qrcode>
作为可下载的图片:
<qrcode data="string" download></qrcode>
作为有链接的二维码:
<qrcode data="http://example.com" href="http://example.com"></qrcode>
download 和 href 互斥,不能同时使用。具体参数入下:
<qrcode version="{{version}}" error-correction-level="{{level}}" size="{{size}}" data="{{var}}" href="{{var}}" color="{{color}}" background="{{background}}" download></qrcode>
三、小结
二维码是个好东西。
推荐:《 Springboot 系列》
欢迎扫一扫我的公众号关注 — 及时得到博客订阅哦!
— http://www.bysocket.com/ —
— https://github.com/JeffLi1993 —
(本文完)

加个微信,送你一份我整理的《100+ 个内容运营工具资料包》