Implementing a page load performance directive in AngularJS

937 Views Asked by At

I am very new with AngularJS so I need quite some pointing in the right direction.

The task is to create some kind of widget that displays how much time it takes from any user action until the requested page finishes rendering.

We are going to be using AngularJS at the presentation layer and the back-end will be Microsoft's Web API.

So I figured I could use the browser's Navigation Timing API and wrap it on an AngularJS directive so I tried this:

angular.module('performanceDirective', [])
   .directive('pagePerformance', function(){
      return {
        restrict: 'AE',
        replace: 'true',
        template: '<div><label id="loadTimeEllapsed">Total Load Time:{{totalLoadTime}}</label></div>',
        scope: {},
        link: function (scope, elem, attrs) {
            scope.$watch('window.performance.timing', function (newValue, oldValue) {
               var timing = window.performance.timing;
               var userTime = timing.loadEventEnd - timing.navigationStart;

               scope.totalLoadTime = userTime;
            });
        }
      };
   });

But it seems that there is something missing because even though I am doing actions that call the back-end the number that gets displayed after the home page loads is never updated.

Is this something that actually would work, provided we fix whatever is failing, or is this a dead end and we need to find another option?

UPDATE

The use of the directive has nothing to it, basically it is just the element thrown on a page:

<body ng-app="myApp">
        <div class="navbar">
            <div class="navbar-inner">
                <ul class="nav">
                    <li><a href="#/someAction">Some Action</a></li>
                </ul>
            </div>
        </div>

    <div class="row">
        <div class="span4"><data-page-performance /></div> <!-- The Directive -->
        <div class="span10" ng-view></div>
    </div>
</body>

Apparently this directive only works if I refresh the page after I have already navigated to it but if I click on an element that will trigger an action on the AngularJS controller the performance number is completely unaffected.

0

There are 0 best solutions below