cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div ng-app="watchDemo">
  <div ng-controller="watchDemoCtrl" class="row">
    <div class="col-md-6">
      <h4 class="label-success white">$scope.$watchCollection - Array</h4>
      <button ng-click="push('X')" class="btn btn-primary btn-sm">Push</button>
      <button ng-click="unshift('X')" class="btn btn-primary btn-sm">Unshift</button>
      <button ng-click="replace('X')" class="btn btn-primary btn-sm">Replace</button>
      <button ng-click="reset()" class="btn btn-warning btn-sm">Reset</button>
    <div class="col-md-12">
      <b class="red">[<span ng-repeat="letter in letters track by $index">{{letter}} </span>]</b>
    </div>

      <div class="col-md-12"><b>[Last called: {{msg.time}}]</b>
      </div>  
      <div class="col-md-12">
        <b>New</b> {{msg.newValue}}
      </div>
      <div class="col-md-12">
        <b>Old</b> {{msg.oldValue}}
      </div>

    </div>
    <div class="col-md-6">  
      <h4 class="label-success white">$scope.$watchCollection - Object</h4>
      <button ng-click="objReplace('a', 'X')" class="btn btn-primary btn-sm">Obj[a] change</button>

      <button ng-click="objReplace('b.ba', 'X')" class="btn btn-primary btn-sm">Obj[b][ba] change</button>

      <button ng-click="objReplace('b.ba.bab', 'X')" class="btn btn-primary btn-sm">Obj[b][ba][bab] change</button>

      <button ng-click="objReplace('b.bb.0.bb1b', 'X')" class="btn btn-primary btn-sm">Obj[a][bb][0][bb1b] change</button>
      <button ng-click="resetObj()" class="btn btn-warning btn-sm">Reset</button>
      
      <div class="col-md-12">
        <span><b class="red">{{obj}}</b></span>
      </div>

      <div class="col-md-12"><b>[Last Called: {{msgObj.time}}]</b>
      </div>  
      <div class="col-md-12"> 
        <b>New</b> {{msgObj.newValue}}
      </div>
      <div class="col-md-12"> 
        <b>Old</b> {{msgObj.oldValue}}
      </div>
    </div>
  </div>
</div>
            
          
!
            
              body {
  overflow-x: hidden;
}

.white {
  color: white;  
}

.red {
  color: red;
}

.center {
  text-align: center;
}
            
          
!
            
              var app = angular.module('watchDemo', []);
app.controller('watchDemoCtrl', ['$scope', function($scope){
  function clone(obj) {
      return JSON.parse(JSON.stringify(obj));
  }
  
  $scope.letters = ['A','B','C'];
  var defaultObj = Object.freeze({
    'a': 1,
    'b': {
      'ba': {
        'bab': 2
      },
      'bb': [
        {
          'bb1a': 3,
          'bb1b': 4
        },
        {
          'bb2a': 5
        }
      ]
    }
  });
  
  //Clone defaultObj to preserve it
  $scope.obj = clone(defaultObj);

  $scope.$watchCollection('letters', function (newValue, oldValue) {
    $scope.msg = {
      time: Date.now(), 
      newValue: clone(newValue),
      oldValue: clone(oldValue)
    };
  });

    $scope.$watchCollection('obj', function (newValue, oldValue) {
    $scope.msgObj = {
      time: Date.now(),
      newValue: clone(newValue),
      oldValue: clone(oldValue)
    };
  });
  
  $scope.push = function (letter) {
    $scope.letters.push(letter);
  };
  
  $scope.unshift = function (letter) {
    $scope.letters.unshift(letter);
  };
  
  $scope.replace = function (letter) {
    $scope.letters[0] = letter;
  }
  
  $scope.reset = function () {
    $scope.letters = ['A', 'B', 'C'];
  }
  
  $scope.resetObj = function () {
    $scope.obj = clone(defaultObj);
  }
  
  $scope.objReplace = function (key) {
    var val = Math.round(Math.random() * 10);
    switch (key) {
      case 'a':
        $scope.obj.a = val;
        break;
      case 'b.ba':
        $scope.obj.b.ba = val;
        break;
      case 'b.ba.bab':
        $scope.obj.b.ba.bab = val;
        break;
      case 'b.bb.0.bb1b':
        $scope.obj.b.bb[0].bb1b = val;
        break;
    }
  }
}]);
            
          
!
999px
Loading ..................

Console