0

Thank you for looking at this,

We recently acquired a project that uses angular 1.x and we're making modifications to the application slowly. However, we noticed that we can't get any logging methods to work when using angular.bootstrap() to start up the application. This also seems to effect other functions on the window object. However, no issue like this exists when bootstrapping with the ng-app directive.

A running code snippet demonstrating this issue can be found here
I'm expecting the console to have hello 1, hello 2, and hello 3 to be printed along with anything in the message input when I click one of the log buttons. I've also included an alert box to demonstrate that the problem isn't limited to logging. However

index.html

<!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>

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);
Gibby
  • 223
  • 1
  • 11

1 Answers1

0

You have to enclose you bootstrap within document.ready, if you setup your scripts as you have done.

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

Otherwise, you need to load your scripts at the end of your page.

Working plunker :

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

Please refer this answer for more details . There are multiple ways of doing this.

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

Vijay Venugopal Menon
  • 1,510
  • 1
  • 14
  • 20