How To Copy Text To Clipboard Using Angular 8

Introduction

 
In this article, we will learn how to copy text to Clipboard and also copy the current date and time, using Angular 8.
 
How will it work?
 
Javascript provides a very good feature to copy any text to the clipboard and can paste it anywhere. So in this article, we will copy some text to clipboard and paste it to textarea. Not only text;  we will also copy the current date and time to clipboard.
 
Prerequisite
  • Basic knowledge of Angular
  • Visual Studio Code must be installed
  • Angular CLI must be installed
  • Node JS must be installed
Step 1
 
Let us create an Angular project, using the following npm command.
  1. ng new CopyToClipboard
Step 2
 
Open the newly-created project in Visual Studio code and install bootstrap in this project.
  1. npm install bootstrap --save
Now, open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.
  1. @import '~bootstrap/dist/css/bootstrap.min.css';
Step 3
 
Now, let's create a new component, by using the following command.
  1. ng g c copy-clipboard
Step 4
 
Now, open copy-clipboard.component.html file and add the following code in this file. 
  1. <div class="card">  
  2.   <div class="card-body pb-0">  
  3.     <div class="row">  
  4.       <div class="col-12 col-md-12">  
  5.         <div class="card">  
  6.   
  7.           <div class="card-header">  
  8.             <label>Copy Text To Clipboard </label>   
  9.          
  10.           </div>  
  11.           <div>  
  12.             <textarea readonly style="margin-left: 317px;text-align: center;" #date>{{todaysDate | date:'dd/MM/yyyy'}}</textarea>  
  13.             <textarea readonly style="margin-left: 317px; text-align: center;" #time>{{todaysDate | date:'HH:mm:ss'}}</textarea>  
  14.           </div>  
  15.           <div class="card-body">  
  16.             <div class="center-form">  
  17.               <div class="row">  
  18.   
  19.                 <div class="col-6 col-md-6">  
  20.                   <div class="form-group">  
  21.                     <h5>Type To Copy</h5>  
  22.   
  23.                     <textarea #userinput placeholder="Enter text to be copied"  
  24.                       style="height: 167px!important;width: 500px;"></textarea>  
  25.                   </div>  
  26.                 </div>  
  27.                 <div class="col-6 col-md-6">  
  28.                   <div class="form-group">  
  29.                     <h5>Paste</h5>  
  30.                     <textarea placeholder="Paste copied text" style="height: 167px!important;width: 500px;"></textarea>  
  31.                   </div>  
  32.                 </div>  
  33.                 <div class="col-12 col-md-12 text-center">  
  34.   
  35.                   <button type="button" style="margin-right:5px" (click)="copyInputMessage(userinput)"  
  36.                     class="btn btn-primary">Copy to  
  37.                     Clipboard</button>  
  38.                   <button type="button" style="margin-right:5px" (click)="copyTodaysDate(date)"  
  39.                     class="btn btn-danger">Copy  
  40.                     Today's Date</button>  
  41.                   <button type="button" style="margin-right:5px" (click)="copyCurrentTime(time)"  
  42.                     class="btn btn-success">Copy  
  43.                     Current Time</button>  
  44.                     <label *ngIf="msgHideAndShow" style="float: right;">{{textMessage}}</label>  
  45.                 </div>  
  46.   
  47.               </div>  
  48.             </div>  
  49.           </div>  
  50.   
  51.         </div>  
  52.       </div>  
  53.     </div>  
  54.   </div>  
  55. </div>  
Step 5
 
Now, open copy-clipboard.component.ts file and add the following code in this file.
  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-copy-clipboard',  
  5.   templateUrl: './copy-clipboard.component.html',  
  6.   styleUrls: ['./copy-clipboard.component.css']  
  7. })  
  8. export class CopyClipboardComponent implements OnInit {  
  9.   todaysDate: Date = new Date();  
  10.   textMessage:any;  
  11.   msgHideAndShow:boolean=false;  
  12.   constructor() {  
  13.     setInterval(() => {  
  14.       this.todaysDate = new Date();  
  15.     }, 1);  
  16.   }  
  17.   
  18.   ngOnInit() {  
  19.     console.log(this.todaysDate);  
  20.   
  21.   }  
  22.   // Text Message   
  23.   textMessageFunc(msgText){  
  24.     this.textMessage=msgText+" Copied to Clipboard";  
  25.     this.msgHideAndShow=true;  
  26.     setTimeout(() => {  
  27.       this.textMessage="";  
  28.       this.msgHideAndShow=false;  
  29.     }, 1000);  
  30.      
  31.   }  
  32.   /* To copy Text from Textbox */  
  33.   copyInputMessage(inputElement) {  
  34.     inputElement.select();  
  35.     document.execCommand('copy');  
  36.     inputElement.setSelectionRange(0, 0);  
  37.     this.textMessageFunc('Text');    
  38.       
  39.   }  
  40.   
  41.   copyTodaysDate(date) {  
  42.     date.select();  
  43.     document.execCommand('copy');  
  44.     date.setSelectionRange(0, 0);  
  45.     this.textMessageFunc('Date');  
  46.   }  
  47.   
  48.   copyCurrentTime(time) {  
  49.     time.select();  
  50.     document.execCommand('copy');  
  51.     time.setSelectionRange(0, 0);  
  52.     this.textMessageFunc('Time');  
  53.   }  
  54. }  
Step 6
 
Now, open app.module.ts file and add the following code in this file.
  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import { FormsModule } from '@angular/forms';  
  4. import { AppComponent } from './app.component';  
  5. import { HttpClientModule } from '@angular/common/http';  
  6. import { CopyClipboardComponent } from './copy-clipboard/copy-clipboard.component';  
  7.   
  8. @NgModule({  
  9.   declarations: [  
  10.     AppComponent,  
  11.     CopyClipboardComponent  
  12.   ],  
  13.   imports: [  
  14.     BrowserModule,  
  15.     HttpClientModule,  
  16.     FormsModule  
  17.   ],  
  18.   providers: [],  
  19.   bootstrap: [AppComponent]  
  20. })  
  21. export class AppModule { }  
Step 7
 
Let's copy the below code and paste it to app.component.html. 
  1. <app-copy-clipboard></app-copy-clipboard>  
Step 8
 
Now let's run the project, by using 'npm start' or 'ng serve' command and check the output.
 
How To Copy Text To Clipboard Using Angular 8
How To Copy Text To Clipboard Using Angular 8 
How To Copy Text To Clipboard Using Angular 8
 

Summary

 
In this article, I have discussed how we can copy any text to the clipboard and paste not only the text, but also the current date and time in Angular 8 applications.
 
Please give your valuable feedback/comments/questions about this article. Please let me know if you liked and understood this article and how I could improve upon it.


Similar Articles