2016-09-06 3 views
2

angular.copy()を使用してログ機能内にargumentsのコピーを作成したいとします。 は既に配列であるため、Arrayを取得する予定ですが、ArrayではなくObjectを返します。これは、コピーの標準的な方法のいくつかの並べ替えsourceが配列の場合、オブジェクトを返すangular.copy()

$scope.log = function(argN) { 
    console.log("arguments", arguments, angular.copy(arguments)); 
    if (typeof(console) !== 'undefined') { 
     console.log.apply(console, angular.copy(arguments)); 
    } 
} 
  1. ですか?
  2. オブジェクトの代わりに配列を取得するにはどうすればよいですか?
+1

'arguments'はアレイではありません。 –

答えて

2

argumentsのような配列が、配列ではありません読んだ後に探しているものだと思います。 argumentsのシャロークローンを作成するには、Array.prototype.slice.call(arguments)を使用します。それはすべての引数の配列を作成します。そこから深いクローンを得るには、angular.copyを使います。

var foo = angular.module('foo', []); 
 

 
foo.controller('bar', function($scope) { 
 
    $scope.trace = function() { 
 
     var clonedArguments = angular.copy(arguments); 
 
     console.log(clonedArguments); 
 
     var clonedArgs = angular.copy(Array.prototype.slice.call(arguments)); 
 
     console.log(clonedArgs); 
 
    }; 
 
    $scope.trace(1, 'foo', { bar: 27 }) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="foo" ng-controller="bar"></div>

関連する問題