2016-10-25 50 views
0

我很困惑$logProvider ... 我已禁用日誌消息,但我仍然得到日誌。

任何人都可以幫我在這段代碼中出錯嗎?

angular 
 
    .module("myModule", []).config(function ($logProvider) { 
 
     $logProvider.debugEnabled(false); 
 
    }) 
 
    .controller("myController", ['$scope','$log', function ($scope, $log) { 
 
     $log.debug("This is sample text"); 
 
     $log.warn("This is sample text"); 
 
     $log.error("This is sample text"); 
 
     $log.info("This is sample text"); 
 
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<body ng-app="myModule" > 
 
    <div ng-controller="myController"> 
 

 
    </div> 
 
</body>

+0

如果它幫助你,可以喲你接受其中一個答案? :) – Mistalis

回答

1

$logProvider.debugEnabled(false);只禁用調試級別的$log,這就是爲什麼你仍然可以使用$log.warn$log.error$log.info

更多關於那here


如果您想徹底關閉$log,檢查這個link,尤其是這部分代碼:

$logProvider.debugEnabled(true); 

$provide.decorator('$log', ['$delegate', function ($delegate) { 
    $delegate.table = angular.noop; 
    return $delegate; 
}]); 
$provide.decorator('$log', ['$delegate', function ($delegate) { 
    $delegate.info = angular.noop; 
    return $delegate; 
}]); 
$provide.decorator('$log', ['$delegate', function ($delegate) { 
    $delegate.warn = angular.noop; 
    return $delegate; 
}]); 
$provide.decorator('$log', ['$delegate', function ($delegate) { 
    $delegate.error = angular.noop; 
    return $delegate; 
}]); 
+0

裝飾者做什麼? – 42HacK

+0

$委託也? – 42HacK

+0

'decorator'用於覆蓋實現。檢查[這個鏈接](https://docs.angularjs.org/guide/decorators),它很好地解釋了裝飾器的工作原理。 – Mistalis

0

希望這有助於你...

angular.module("myModule", []) 
 
    .config(['$provide', 
 
     function($provide) { 
 
     $provide.decorator('$log', ['$delegate', 
 
      function($delegate) { 
 
      var origDebug = $delegate.debug; 
 

 
      $delegate.debug = function() { 
 
       var args = [].slice.call(arguments); 
 
       args[0] = [new Date().toString(), ': ', args[0]].join(''); 
 
       origDebug.apply(null, args) 
 
      }; 
 

 
      return $delegate; 
 
      } 
 
     ]); 
 
     } 
 
    ]) 
 

 
    .controller("myController", ['$scope', '$log', 
 
    function($scope, $log) { 
 
     $log.debug("This is sample text"); 
 
     $log.warn("This is sample text"); 
 
     $log.error("This is sample text"); 
 
     $log.info("This is sample text"); 
 
    } 
 
    ]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script> 
 
<div ng-app="myModule"> 
 
    <div ng-controller="myController"> 
 

 
    </div> 
 

 
</div>