DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • Send Email Using Spring Boot (SMTP Integration)
  • Tactics and Strategies on Software Development: How To Reach Successful Software [Video]
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective

Trending

  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • Send Email Using Spring Boot (SMTP Integration)
  • Tactics and Strategies on Software Development: How To Reach Successful Software [Video]
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective
  1. DZone
  2. Coding
  3. Frameworks
  4. How To Copy Text to Clipboard Using Angular 8

How To Copy Text to Clipboard Using Angular 8

Siddharth Gajbhiye user avatar by
Siddharth Gajbhiye
CORE ·
Apr. 01, 20 · Tutorial
Like (4)
Save
Tweet
Share
43.35K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

In this article, we will learn how to copy text to our clipboard and also copy the current date and time, using Angular 8.

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 and the current date and time to our clipboard and paste it to a textarea. 

Prerequisites

  • Basic knowledge of Angular.
  • Visual Studio Code must be installed.
  • Angular CLI must be installed.
  • Node JS must be installed.

Step 1

Let's create an Angular project, using the following npm command.

TypeScript
xxxxxxxxxx
1
 
1
ng new CopyToClipboard


Step 2

Open the newly-created project in Visual Studio code and install bootstrap in this project.

TypeScript
xxxxxxxxxx
1
 
1
npm install bootstrap --save


Now, open the styles.css file and add a Bootstrap file reference. To add the reference in styles.css file add this line:

TypeScript
xxxxxxxxxx
1
 
1
@import '~bootstrap/dist/css/bootstrap.min.css';


Step 3 

Now, let's create a new component by using the following command:

TypeScript
xxxxxxxxxx
1
 
1
ng g c copy-clipboard


Step 4

Now, open the copy-clipboard.component.html file and add the following code: 

HTML
xxxxxxxxxx
1
55
 
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 the copy-clipboard.component.ts file and add the following code:

TypeScript
xxxxxxxxxx
1
54
 
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 the app.module.ts file and add the following code in this file.

TypeScript
xxxxxxxxxx
1
21
 
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. 

HTML
 




xxxxxxxxxx
1


 
1

          
2
<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.

 Copying text to clipboard

Copying current date to clipboard

Copying current time to clipboard

 

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.

AngularJS

Opinions expressed by DZone contributors are their own.

Trending

  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • Send Email Using Spring Boot (SMTP Integration)
  • Tactics and Strategies on Software Development: How To Reach Successful Software [Video]
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: