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

Commit ad31f23

Browse files
committed
add ngRefElement, update docs, update tests
1 parent 6ddda90 commit ad31f23

File tree

2 files changed

+229
-129
lines changed

2 files changed

+229
-129
lines changed

src/ng/directive/ngRef.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
* @restrict A
77
*
88
* @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.
1112
*
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.
1314
*
1415
*
1516
* @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.
1721
*
1822
*
1923
* @example
@@ -189,17 +193,24 @@ var ngRefDirective = ['$parse',function($parse) {
189193
var getter = $parse(tAttrs.ngRef);
190194
var setter = getter.assign;
191195

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);
197208

198209
// when the element is removed, remove it (nullify it)
199210
element.on('$destroy', function() {
200211
// only remove it if value has not changed,
201212
// carefully because animations (and other procedures) may duplicate elements
202-
if (getter(scope) === value) {
213+
if (getter(scope) === refValue) {
203214
setter(scope, null);
204215
}
205216
});

0 commit comments

Comments
 (0)