Accessing a window from an Angular expression

According to the developer's guide, I should have access to the browser window from inside Angular c expressions $window.

Unlike JavaScript, where the default names correspond to global window properties, Angular expressions must use $ window to refer to the global window object. For example, if you want to call alert (), which is defined in a window, you must use $ window.alert () in the expression.

However, I cannot access $windowfrom the expressions evaluated with $scope.$eval. Here are some results that I get when I exit the console:

console.log($window);                   // the Window object as expected
console.log($scope.$eval('$window'));   // undefined
console.log($scope.$eval('1+1'));       // 2
console.log($scope.$eval('scopeVar'));  // 'abc'

The controller has $windowboth a dependency. I can access scope variables and other services from expressions, but not $window, so $scope.$eval($window.alert())it doesn't work either.

What am I missing here?

+4
source share
1 answer

$scope.$evalis rated using $scope, so your rating will only work if you assign the $ window service to a member of the scope:

$scope.$window = $window;  
console.log($scope.$eval('$window'));
+6
source

All Articles