我创建了一个有效的CodePen示例,演示了如何在AngularJS中正确执行此操作。Angular$window服务应该用于访问任何全局对象,因为直接访问
window会使测试更加困难。
HTML:
<section ng-app="myapp" ng-controller="MainCtrl"> Value of global variable read by AngularJS: {{variable1}}</section>Javascript:
// global variable outside angularvar variable1 = true;var app = angular.module('myapp', []);app.controller('MainCtrl', ['$scope', '$window', function($scope, $window) { $scope.variable1 = $window.variable1;}]);


