[英]Angular.bootstrap() and logging
謝謝你看這個
我們最近獲得了一個使用angular 1.x的項目,並且正在對應用程序進行緩慢的修改。 但是,我們注意到,使用angular.bootstrap()
啟動應用程序時,無法使用任何日志記錄方法。 這似乎也會影響window對象上的其他功能。 但是,使用ng-app指令進行引導時,不會出現這樣的問題。
可以在此處找到正在運行的代碼段,以證明此問題。
我希望單擊日志按鈕之一時,控制台將打印出hello 1,hello 2和hello 3以及消息輸入中的所有內容。 我還包括一個警告框,以說明問題不僅限於日志記錄。 然而
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example110-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="LogController">
<p>Reload this page with open console, enter text and hit the log button...</p>
<label>Message:
<input type="text" ng-model="message" />
</label>
<button ng-click="alert()">alert</button>
<button ng-click="$log.log(message)">log</button>
<button ng-click="$log.warn(message)">warn</button>
<button ng-click="$log.info(message)">info</button>
<button ng-click="$log.error(message)">error</button>
<button ng-click="$log.debug(message)">debug</button>
</div>
</body>
</html>
(function (angular) {
'use strict';
console.log('hello1')
angular.module('logExample', []);
angular.module('logExample')
.controller('LogController', ['$window', '$scope', '$log', function ($window, $scope, $log) {
$scope.$log = $log;
$scope.message = 'Hello World!';
$log.log('hello2');
$window.console.log('hello3')
$scope.alert = function () {
$window.alert('hi')
}
}]);
angular.bootstrap(document.body, ['logExample']);
})(window.angular);
如果您已完成腳本的設置,則必須將引導程序包含在document.ready中。
angular.element(document).ready(function() {
angular.bootstrap(document, ['logExample']);
})
否則,您需要在頁面末尾加載腳本。
工作鑽頭:
http://plnkr.co/edit/LZDjnAGMlU3Npoetdg2U?p=preview
請參閱此答案以獲取更多詳細信息。 有多種方法可以做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.