Generate QR code using AngularJS
Last Updated :
10 Mar, 2021
In this article, we will see how to generate and display QR codes in our Angular apps. A QR code is a matrix of black and white squares that can be read by a camera or a smartphone. A QR code can store information and URLs that make it easy to read for a bot or smartphone user. In a business scenario, QR codes can be used to share contact information, send emails, download apps, share URLs and location. Hence, we need to know how to generate one for our apps to keep up with the market.
Prerequisites: NPM must be installed
Environment Setup:
Install angular CLI and create a new angular project.
npm i -g @angular/cli
ng new <project-name>
cd <project-name>
Now, verify the installation by serving the app on localhost:
ng serve -o
You should see the landing page of angular, and you are good to go. Now we need to install and register an additional package:
npm install @techiediaries/ngx-qrcode
Register it in app.module.ts. Make changes or copy the code below in app.module.ts file in app folder.
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
// Import this module
import {NgxQRCodeModule} from '@techiediaries/ngx-qrcode'
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
NgxQRCodeModule // Register the module
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Now lets create a new component to display the QR code with the required data.
ng generate component qrcode
This will generate 4 new files. Additionally, it will update the file app.module.ts by registering the component automatically. Now add the following code in qrcode.component.ts :
qrcode.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-qrcode',
templateUrl: './qrcode.component.html',
styleUrls: ['./qrcode.component.css']
})
export class QrcodeComponent{
elementType = 'url';
data = 'https://geeksforgeeks.org/';
}
Here elementType can be 'url', 'img' or 'canvas'. The url type can encode string type data. The data variable stores the data we want to encode. Now add the following code to qrcode.component.html:
qrcode.component.html
<ngx-qrcode
[elementType]="elementType"
[value]="data">
</ngx-qrcode>
We have used the ngx-qrcode tag to place a component. It takes the previous data as input. Now add this component in app.component.html :
app.component.html
<div>
<app-qrcode></app-qrcode>
</div>
We can check it by starting the app :
ng serve -o
Open http://localhost:4200/ on your browser. You should see the following output. You can validate it by scanning the code using any app.
Encoding the JSON data: We can also embed JSON data into the QR code. First we need to create object that we want to embed. Note that we can only embed string data when using 'url' elementType. So we can create a string of this object using JSON.stringify(). See the code below for qrcode.component.ts to understand better:
qrcode.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-qrcode',
templateUrl: './qrcode.component.html',
styleUrls: ['./qrcode.component.css']
})
export class QrcodeComponent{
elementType = 'url';
obj = {
cellTowers: [
{
cellId: 170402199,
locationAreaCode: 35632,
mobileCountryCode: 310,
mobileNetworkCode: 410,
age: 0,
signalStrength: -60,
timingAdvance: 15
}
]
}
data = JSON.stringify(this.obj);
}
Output:
Â
Similar Reads
Angular ng generate
The ng generate command is one of the most commonly used commands in Angular CLI. It is used to generate various Angular elements like components, services, modules, pipes, and more. This command helps maintain consistency across the project by automatically creating the necessary files and updating
10 min read
How to encode/decode URL using AngularJS ?
In this article, we will see the mechanism to encode/decode URL using AngularJS, along with knowing the different methods available to accomplish the given task, & will understand it through the illustration. A URL specifies a resource and its access protocol.Encode URL: URL Encoding is a way to
3 min read
Quote Generator App Using Angular
A Quote Generator App is a simple application that displays random quotes to users. It is a great project for practising Angular basics such as components, services, data and API integration.Here we develop a simple Quote Generator App using Angular. This application can able to display a new Quote
6 min read
How to generate QR Codes with Angular 10 ?
QR Code (Quick Response Code) has become the essential component for each and every product, organization, app, etc. In marketing campaigns, we can use the generated QR code to Download appsSend EmailView business location etc. In this article let us see how to generate QR Codes with Angular 10. Pre
4 min read
Meme Generator App using Angular
Angular has become one of the, most popular frameworks for building dynamic web applications. In this project, we will build a simple yet entertaining Meme generator app using Angular. The app will fetch random memes from a public API and display them to the user. This project will help us understan
5 min read
Joke generator App Using Angular
Angular has become one of the most popular frameworks for building dynamic web applications. In this project, we build a simple yet entertaining Joke generator app using Angular. The app will fetch random jokes from a public API and display them to the user. This project will help us understand how
3 min read
Event Calender using Angular
In this article, we will walk through the steps to create a dynamic Event Calendar using Angular 17 standalone components. This calendar will allow users to add events to specific dates and view them interactively. We will utilize Angular's powerful features and best practices to build a clean and e
6 min read
How to concat strings using AngularJS ?
In this article, we will see how to concat strings in AngularJS. There are few ways to concat the strings in AngularJS. In this article, we will see 2 of them.Example 1: In the first example, we are using the '+' operator to concat the strings HTML<!DOCTYPE HTML> <html> <head> <
2 min read
QR Code Generator using HTML, CSS and jQuery
In this article, we will learn how to build a QR generator using HTML, CSS, and jQuery. A QR code generator is an application that stores any required textual data into a QR code which can be later scanned with a QR code scanner to reveal the stored information. This code generator consists of an in
3 min read
Format a Date using ng-model in AngularJS
The ng-model directive is used to bind the value of the input field to a property made in the controller. Formatters are used to convert the value of input from one textural representation to another desired representation. Formatters used for date formatting are useful when the date needs to be upd
3 min read