Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit abd6889

Browse files
committed
docs(guide/expression): add section on $event
1 parent 074648e commit abd6889

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

docs/content/guide/expression.ngdoc

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
@name Expressions
33
@description
44

5-
Expressions are JavaScript-like code snippets that are usually placed in bindings such as
5+
# Angular Expressions
6+
7+
Angular expressions are JavaScript-like code snippets that are usually placed in bindings such as
68
`{{ expression }}`.
79

810
For example, these are valid expressions in Angular:
@@ -88,7 +90,7 @@ You can try evaluating different expressions here:
8890
</example>
8991

9092

91-
# Context
93+
## Context
9294

9395
Angular does not use JavaScript's `eval()` to evaluate expressions. Instead Angular's
9496
{@link ng.$parse $parse} service processes these expressions.
@@ -154,3 +156,44 @@ You cannot write a control flow statement in an expression. The reason behind th
154156
Angular philosophy that application logic should be in controllers, not the views. If you need a
155157
conditional, loop, or to throw from a view expression, delegate to a JavaScript method instead.
156158

159+
## `$event`
160+
161+
Directives like {@link ng.directive:ngClick `ngClick`} and {@link ng.directive:ngFocus `ngFocus`}
162+
expose a `$event` object within the scope of that expression.
163+
164+
<example module="eventExampleApp">
165+
<file name="index.html">
166+
<div ng-controller="EventController">
167+
<button ng-click="clickMe($event)">Event</button>
168+
<p><code>$event</code>: <pre> {{$event | json}}</pre></p>
169+
<p><code>clickEvent</code>: <pre>{{clickEvent | json}}</pre></p>
170+
</div>
171+
</file>
172+
173+
<file name="script.js">
174+
angular.module('eventExampleApp', []).
175+
controller('EventController', ['$scope', function($scope) {
176+
/*
177+
* expose the event object to the scope
178+
*/
179+
$scope.clickMe = function(clickEvent) {
180+
$scope.clickEvent = simpleKeys(clickEvent);
181+
console.log(clickEvent);
182+
};
183+
184+
/*
185+
* return a copy of an object with only non-object keys
186+
* we need this to avoid circular references
187+
*/
188+
function simpleKeys (original) {
189+
return Object.keys(original).reduce(function (obj, key) {
190+
obj[key] = typeof original[key] === 'object' ? '{ ... }' : original[key];
191+
return obj;
192+
}, {});
193+
}
194+
}]);
195+
</file>
196+
</example>
197+
198+
Note in the example above how we can pass in `$event` to `clickMe`, but how it does not show up
199+
in `{{$event}}`. This is because `$event` is outside the scope of that binding.

0 commit comments

Comments
 (0)