Page 144 - AngularJS权威教程
P. 144

124  第 14 章  服务


                 例如,下面的代码展示了如何给githubService添加装饰器,从而为每个请求都加上一个时
             间戳:

                 var githubDecorator = function($delegate,$log) {
                     var events = function(path) {
                         var startedAt = new Date();
                         var events = $delegate.events(path);
                         // 事件是一个promise events.finally(function() {
                             $log.info("Fetching events" +
                                 " took " +
                                 (new Date() - startedAt) + "ms");
                          });
                         return events;
                     };

                     return {
                         events: events
                     };
                 };

                 angular.module('myApp')
                 .config(function($provide) {
                     $provide.decorator('githubService',githubDecorator);
                 });
   139   140   141   142   143   144   145   146   147   148   149