angular.Object.copy

Description

Creates a deep copy of source, which should be an object or an array.

Note: this function is used to augment the Object type in Angular expressions. See angular.Array for more information about Angular arrays.

Usage

angular.Object.copy(source[, destination]);

Parameters

Returns

{*}

The copy or updated destination, if destination was specified.

Example

    <script>
      function Ctrl() {
        this.master = {
          salutation: 'Hello',
          name: 'world'
        };
        this.copy = function() {
          this.form = angular.copy(this.master);
        }
      }
    </script>
    <div ng:controller="Ctrl">
      Salutation: <input type="text" ng:model="master.salutation" ><br/>
      Name: <input type="text" ng:model="master.name"><br/>
      <button ng:click="copy()">copy</button>
      <hr/>

      The master object is <span ng:hide="master.$equals(form)">NOT</span> equal to the form object.

      <pre>master={{master}}</pre>
      <pre>form={{form}}</pre>
    </div>
  it('should print that initialy the form object is NOT equal to master', function() {
    expect(element('.doc-example-live input[ng\\:model="master.salutation"]').val()).toBe('Hello');
    expect(element('.doc-example-live input[ng\\:model="master.name"]').val()).toBe('world');
    expect(element('.doc-example-live span').css('display')).toBe('inline');
  });

  it('should make form and master equal when the copy button is clicked', function() {
    element('.doc-example-live button').click();
    expect(element('.doc-example-live span').css('display')).toBe('none');
  });