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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Trending

  • Scalability 101: How to Build, Measure, and Improve It
  • Fixing Common Oracle Database Problems
  • Virtual Threads: A Game-Changer for Concurrency
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  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

By 
Siddharth Gajbhiye user avatar
Siddharth Gajbhiye
·
Apr. 01, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
45.3K 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.

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!