Angular.bootstrap() and logging Angular.bootstrap()是AngularJS框架中的一个函数,它用于手动启动应用程序。通过调用这个函数,我们可以在非自动启动的情况下手动启动Angular应用程序。 Logging(记录日志)是一个用于记录应用程序运行时信息的重要工具。通过记录日志,我们可以追踪和调试应用程序中的错误和异常。在Angular中,我们可以使用内置的$log服务来记录日志,并将其输出到控制台或其他日志记录工具中。

9 浏览
0 Comments

Angular.bootstrap() and logging Angular.bootstrap()是AngularJS框架中的一个函数,它用于手动启动应用程序。通过调用这个函数,我们可以在非自动启动的情况下手动启动Angular应用程序。 Logging(记录日志)是一个用于记录应用程序运行时信息的重要工具。通过记录日志,我们可以追踪和调试应用程序中的错误和异常。在Angular中,我们可以使用内置的$log服务来记录日志,并将其输出到控制台或其他日志记录工具中。

感谢您关注此事,最近我们收购了一个使用Angular 1.x的项目,并且我们正在逐步修改该应用程序。然而,我们注意到当使用angular.bootstrap()来启动应用程序时,无法使任何日志方法正常工作。这似乎也会影响窗口对象上的其他函数。然而,使用ng-app指令进行引导时不存在此类问题。

可以在此处找到演示此问题的运行代码片段:here

我期望控制台打印出hello 1、hello 2和hello 3,以及点击日志按钮时输入框中的任何内容。我还包括了一个警告框来证明问题不仅限于日志记录。然而

index.html




    
    Example - example-example110-production
    
    


        Reload this page with open console, enter text and hit the log button...
        
        
        
        
        
        
        


script.js

(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);

0
0 Comments

问题的原因是需要将bootstrap方法包裹在document.ready中,如果脚本设置如上所示。否则,需要将脚本加载在页面的末尾。

解决方法是使用下面的代码将bootstrap方法包裹在document.ready中:

angular.element(document).ready(function() {
  angular.bootstrap(document, ['logExample']);
})

如果想要了解更多细节,请参考这个答案:

https://stackoverflow.com/a/16539428/6347317

你也可以在这个链接上找到一个可行的示例:

http://plnkr.co/edit/LZDjnAGMlU3Npoetdg2U?p=preview

非常感谢提供的参考链接。

0