AngularJS Live Example

by mhevery

HTML

<script src="http://code.angularjs.org/1.0.0rc2/angular-1.0.0rc2.js"></script>
<div ng:app="instrument">
 <div ng:controller="HelloCntl">
   Your name: <input type="text" ng:model="name" value="World"/>
   <hr/>
   Hello {{name}}!
 </div>
</div>

JavaScript

function HelloCntl() {
  this.name = 'World';
}

angular.module('instrument', [])
    .config(function($provide){
        var intercept = function(methodName, method) {
            return function(a1, a2, a3) {
                if (methodName == '$watch') {
                    method.call(this, function(scope){
                        var start = new Date().getTime();
                        try {
                          return typeof a1 == 'string' ? scope.$eval(a1) : a1.apply(this.arguments);
                        } finally {
                          console.log('$watch exp:', a1, new Date().getTime() - start);
                        }                                  
                    }, function(n, o, s) {
                        var start = new Date.getTime();
                        try {
                          return a2 && a2.apply(this, argumets);
                        } finally {
                          console.log('$watch fire:', a2, new Date().getTime() - start);
                        }                                  
                    }, a3);
                } else {
                    console.log(methodName + '(', arguments, ')');
                    return method.apply(this, arguments);
                }
            }
        }

        $provide.decorator('$rootScope', function($delegate){
            var $rootScope = $delegate;
            var prototype = $rootScope.__proto__;
            angular.forEach(prototype, function(fn, key) {
              prototype[key] = intercept(key, fn);
            })
            return $rootScope;
        });
    
    });