0

being a newbie to angular got stuck in copying json data, I need to copy my entire json object to clipboard , and then paste it later on. have done it through ngClip , but it dosen't copy the entire object else it copies the object name.below is what I've done so far:

<label>Copy #1</label>

<button class="btn btn-default" clip-copy="copy1">Copy!</button>
<script>
   var myapp = angular.module('myapp', ["ngClipboard"]);

   myapp.config(['ngClipProvider', function(ngClipProvider) {
      ngClipProvider.setPath("//cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.1.6/ZeroClipboard.swf");
   }]);

   myapp.controller('myctrl', function($scope) {
      $scope.copy1 = {};
      $scope.copy1.Name = "ABC";
      $scope.copy1.age = 24;
      $scope.copy1.City = "Pittsburgh";

   });
</script>

Clicking on Copy! button always copies copy1, I want it to copy the entire object.However if I give copy1.Name in clip-copy it copies ABC. Any leads wud be appreciated.

5
  • Can you try converting the object into a json string before you put into copy1? Commented Aug 27, 2015 at 11:10
  • @Icycool : please see the edited one, it copies the name of the object, nothing else, tried with JSON.stringify also. Commented Aug 27, 2015 at 11:13
  • docs.angularjs.org/api/ng/function/angular.copy? Commented Aug 27, 2015 at 11:19
  • stackoverflow.com/questions/26411754/… Commented Aug 27, 2015 at 11:20
  • @Fissio: have seen this link, but I guess angular.copy or clone makes a copy of the object locally, I want it to be copied to the clipboard. Commented Aug 27, 2015 at 11:23

3 Answers 3

2

Use JSON.stringify(obj) to convert your object to string. It will return you a string which you can copy on clip board.

Sign up to request clarification or add additional context in comments.

Comments

2

It's probably because copy1 is an object. Try using angular.toJson() to convert the object to a string. Like this:

<label>Copy #1</label>

<button class="btn btn-default" clip-copy="test()">Copy!</button>
<script>
   var myapp = angular.module('myapp', ["ngClipboard"]);

   myapp.config(['ngClipProvider', function(ngClipProvider) {
      ngClipProvider.setPath("//cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.1.6/ZeroClipboard.swf");
   }]);

   myapp.controller('myctrl', function($scope) {
      $scope.copy1 = {};
      $scope.copy1.Name = "ABC";
      $scope.copy1.age = 24;
      $scope.copy1.City = "Pittsburgh";

      $scope.test = function () {
        return angular.toJson($scope.copy1);
      }

   });
</script>

Plunker: http://plnkr.co/edit/KISecLdBfOMxVPFT6MMZ?p=preview

Comments

0

Use angular.copy($scope.copy1,$scope.target) . it will copy your entire copy1 object to target object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.