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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building Enterprise Authentication With Angular, Spring Boot, SSO, and MFA
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems

Trending

  • Reading GC Logs: Memory Leak or Undersized Heap?
  • Modernizing a Legacy Java Application With OpenRewrite: What We Automated and What We Didn't
  • How (and Why) to Build Dynamic Data Pipelines
  • We’re Not Paid to Practice Scrum, but Answer 5 Product Questions

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Apr. 01, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
45.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Building Enterprise Authentication With Angular, Spring Boot, SSO, and MFA
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook