|
6 | 6 | * @restrict A
|
7 | 7 | *
|
8 | 8 | * @description
|
9 |
| - * The `ngRef` attribute tells AngularJS to assign the element component controller |
10 |
| - * to the given property in the current scope. |
| 9 | + * The `ngRef` attribute tells AngularJS to assign the controller of a component (or an element-directive) |
| 10 | + * to the given property in the current scope. If no controller exists, the jqlite-wrapped DOM |
| 11 | + * element will be added to the scope. |
11 | 12 | *
|
12 |
| - * If the component is destroyed `null` is assigned to the property. |
| 13 | + * If the element with ngRef is destroyed `null` is assigned to the property. |
13 | 14 | *
|
14 | 15 | *
|
15 | 16 | * @element ANY
|
16 |
| - * @param {string} ngRef property name - this must be a valid AngularJS expression identifier |
| 17 | + * @param {string} ngRef property name - A valid AngularJS expression identifier to which the |
| 18 | + * controller or jqlite-wrapped DOM element will be bound. |
| 19 | + * @param {string=} ngRefElement void - If specified, ngRef will always bind the jqlite-wrapped |
| 20 | + * DOM-element even if a controller is available. |
17 | 21 | *
|
18 | 22 | *
|
19 | 23 | * @example
|
@@ -189,17 +193,24 @@ var ngRefDirective = ['$parse',function($parse) {
|
189 | 193 | var getter = $parse(tAttrs.ngRef);
|
190 | 194 | var setter = getter.assign;
|
191 | 195 |
|
192 |
| - return function(scope, element) { |
193 |
| - // gets the controller of the current component or the current DOM element |
194 |
| - var controller = element.data('$' + controllerName + 'Controller'); |
195 |
| - var value = controller || element[0]; |
196 |
| - setter(scope, value); |
| 196 | + return function(scope, element, attrs) { |
| 197 | + var refValue = null; |
| 198 | + |
| 199 | + if ('ngRefElement' in attrs) { |
| 200 | + refValue = element; |
| 201 | + } else { |
| 202 | + // gets the controller of the current component or the current DOM element |
| 203 | + var controller = element.data('$' + controllerName + 'Controller'); |
| 204 | + refValue = controller || element; |
| 205 | + } |
| 206 | + |
| 207 | + setter(scope, refValue); |
197 | 208 |
|
198 | 209 | // when the element is removed, remove it (nullify it)
|
199 | 210 | element.on('$destroy', function() {
|
200 | 211 | // only remove it if value has not changed,
|
201 | 212 | // carefully because animations (and other procedures) may duplicate elements
|
202 |
| - if (getter(scope) === value) { |
| 213 | + if (getter(scope) === refValue) { |
203 | 214 | setter(scope, null);
|
204 | 215 | }
|
205 | 216 | });
|
|
0 commit comments