AngularJS ng-copy Directive

Last Updated : 8 Aug, 2022

The ng-copy Directive in AngularJS is used to specify the custom behavior functions during the copy of the text in input text fields. It can be used when we want to call a function that will be triggered when the text is copied from the input field. It is supported by all input elements. The element's original oncopy event will not be overridden with the ng-copy directive when both are executing.

Syntax:

 
    Contents... 

Parameter:

  • expression: It specifies what to do when the element is copied.

Example 1: This example uses the ng-copy Directive to display a message when an element will copy. 

HTML

<html>

<head>
    <title>ng-copy Directivetitle>
    <script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js">
    script>
    <style>
        body {
            text-align: center;
            font-family: sans-serif;
        }

        h1 {
            color: green;
        }
    style>
head>

<body ng-app="">
    <h1>GeeksforGeeksh1>
    <h2>ng-copy Directiveh2>
    <div ng-init="isCopy=false; copy='Copy this text!'">
        <textarea ng-copy="isCopy=true" ng-model="copy">
        textarea><br>
        <pre>Copied status: {{isCopy}}pre>
    div>
body>
html>

Output:

 

Example 2: This example describes the use of the ng-copy Directive in AngularJS where it specifies that the number of times the given text will be copied, its total count will be displayed accordingly.

HTML

<html>

<head>
    <title>ng-copy Directivetitle>
    <script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
    script>
    <style>
        body {
            text-align: center;
            font-family: sans-serif;
        }

        h1 {
            color: green;
        }
    style>
head>

<body ng-app="">
    <h1>GeeksforGeeksh1>
    <h2>ng-copy Directiveh2>
    <p>Copy the "GeeksforGeeks" textp>
    <input ng-copy="copy = copy + 1" 
           ng-init="copy=0" 
           value="GeeksforGeeks" />
    <p> {{copy}} times text copied.p>
body>
html>

Output:

 
Comment

Explore