2016-06-25 17 views
-2

JavaScript-Funktion kopiert Text aus dem Eingabefeld in die Zwischenablage, wenn Sie auf die Schaltfläche klicken. Es sieht aus wie folgt:Wie konvertiert man die JavaScript-Funktion in die AngularJS-Direktive?

<script type="text/javascript"> 
    (function() { 
     'use strict'; 
     // click events 
     document.body.addEventListener('click', copy, true); 
     // event handler 
     function copy(e) { 
      // find target element 
      var 
       t = e.target, 
       c = t.dataset.copytarget, 
       inp = (c ? document.querySelector(c) : null); 
      // is element selectable? 
      if (inp && inp.select) { 
       // select text 
       inp.select(); 
       try { 
        // copy text 
        document.execCommand('copy'); 
        inp.blur(); 
       } 
       catch (err) { 
        alert('please press Ctrl/Cmd+C to copy'); 
       } 
      } 
     } 
    })(); 
</script> 

Verbrauch:

<button id="CopyTextBtn" autofocus 
     type="submit" 
     class="uui-button lime-green" 
     data-copytarget="#ClientsURL" 
     ng-click="closeThisDialog('Cancel')"> 
    Copy 
</button> 

ich das versucht habe:

appModule.directive('data-copytarget', function() { 
    return { 
     scope: {}, 
     link: function (scope, element) 
     { 
      // click events 
      document.body.addEventListener('click', copy, true); 
      // event handler 
      function copy(e) { 
       // find target element 
       var 
        t = e.target, 
        c = t.dataset.copytarget, 
        inp = (c ? document.querySelector(c) : null); 
       // is element selectable? 
       if (inp && inp.select) { 
        // select text 
        inp.select(); 
        try { 
         // copy text 
         document.execCommand('copy'); 
         inp.blur(); 
        } 
        catch (err) { 
         alert('please press Ctrl/Cmd+C to copy'); 
        } 
       } 
      } 
     } 
    }; 
}); 

Aber es funktioniert nicht.

+2

* "Es funktioniert nicht" * ist keine richtige technische Problemstellung. Setzen Sie ein wenig Mühe darauf. Siehe [fragen] – charlietfl

+0

Natürlich habe ich es getan. – tesicg

+0

Huh? Hat was gemacht? Sicher hat sich keine Mühe gegeben, eine korrekte Beschreibung mit irgendwelchen Fehlerbehebungsinformationen zu geben – charlietfl

Antwort

0

Sie haben Scope, Element und attrs als Argument in Link-Funktion, warum nicht elem.bind('click', function() { }) verwenden oder Sie können angular.element(document).find('body'); verwenden und dann das Click-Ereignis daran binden.

appModule.directive('data-copytarget', function() { 
    return { 
     scope: {}, 
     link: function (scope, elem) 
     { 
      // click events 
      elem.bind('click', function() { 
       // find target element 
       var 
        t = e.target, 
        c = t.dataset.copytarget, 
        inp = (c ? document.querySelector(c) : null); 
       // is element selectable? 
       if (inp && inp.select) { 
        // select text 
        inp.select(); 
        try { 
         // copy text 
         document.execCommand('copy'); 
         inp.blur(); 
        } 
        catch (err) { 
         alert('please press Ctrl/Cmd+C to copy'); 
        } 
       }     
     } 
    }; 
}); 
+0

Leider funktioniert es nicht. Die Anweisung sollte Text aus dem Eingabefeld in die Zwischenablage kopieren. – tesicg

+0

Es ist interessant, dass auf Knopfdruck der Programmablauf überhaupt nicht in die Direktive eingeht. – tesicg

Verwandte Themen