Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09367d8

Browse files
timrufflesbtford
authored andcommitted
docs(guide/expression): fix docs re $window
The documentation on context is incorrect and misleading: 1. "Angular expressions must use $window explicitly to refer to the global `window` object": expressions cannot access `$window` 1. The example doesn't actually attempt to use $window in a expression. It's in a function called from an expression, which incorrectly implies to readers that: 1. functions ARE expressions 1. functions called by expressions can't access `window` Here's [a plunkr](http://plnkr.co/edit/Gd4xAV?p=preview) to make both these issues clear. This change fixes the errors and informs the reader about Angular's `$window` etc services, and adds an explicit example of an expression not being able to access `window`.
1 parent 369f69d commit 09367d8

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

docs/content/guide/expression.ngdoc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,18 @@ You can try evaluating different expressions here:
9595
Angular does not use JavaScript's `eval()` to evaluate expressions. Instead Angular's
9696
{@link ng.$parse $parse} service processes these expressions.
9797

98-
Unlike JavaScript, where names default to global `window` properties, Angular expressions must use
99-
{@link ng.$window `$window`} explicitly to refer to the global `window` object. For example, if you
100-
want to call `alert()` in an expression you must use `$window.alert()`. This restriction is
101-
intentional. It prevents accidental access to the global state – a common source of subtle bugs.
98+
Angular expressions do not have access to global variables like `window`, `document` or `location`.
99+
This restriction is intentional. It prevents accidental access to the global state – a common source of subtle bugs.
100+
101+
Instead use services like `$window` and `$location` in functions called from expressions. Such services
102+
provide mockable access to globals.
102103

103104
<example>
104105
<file name="index.html">
105106
<div class="example2" ng-controller="Cntl1">
106107
Name: <input ng-model="name" type="text"/>
107108
<button ng-click="greet()">Greet</button>
109+
<button ng-click="window.alert('Should not see me')">Won't greet</button>
108110
</div>
109111
</file>
110112

0 commit comments

Comments
 (0)