forked from SuperiorJT/angular2-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular2-qrcode.ts
61 lines (52 loc) · 1.44 KB
/
angular2-qrcode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {
NgModule,
Component,
Input,
Output,
ElementRef,
OnChanges,
SimpleChanges
} from '@angular/core';
import * as qrcode from "qrcode-generator";
@Component({
moduleId: 'module.id',
selector: 'qr-code',
template: ''
})
export class QRCodeComponent implements OnChanges {
ngOnChanges(changes: SimpleChanges): void {
if ('data' in changes) {
this.generate();
}
}
@Input() data: string = '';
@Input() size: number = 128;
@Input() type: number = 4;
@Input() level: string = 'M';
@Output() isReady: boolean = false;
constructor(private elementRef: ElementRef) {
}
generate() {
try {
let qr = qrcode(this.type, this.level);
qr.addData(this.data);
qr.make();
let imgTagString = qr.createImgTag(this.type, 0);
let el: HTMLElement = this.elementRef.nativeElement;
el.innerHTML = imgTagString;
let imgTagObject: HTMLImageElement = <HTMLImageElement> el.firstElementChild;
imgTagObject.width = this.size;
imgTagObject.height = this.size;
this.isReady = true;
} catch (e) {
console.error(`Could not generate QR Code: ${e.message}`);
}
}
}
@NgModule({
exports: [QRCodeComponent],
declarations: [QRCodeComponent],
entryComponents: [QRCodeComponent]
})
export class QRCodeModule {
}