|
177 | 177 | * </example>
|
178 | 178 | *
|
179 | 179 | */
|
180 |
| -var ngRefDirective = function() { |
181 |
| - var ngRefMinErr = minErr('ngRef'); |
182 |
| - |
| 180 | +var ngRefDirective = ['$parse',function($parse) { |
183 | 181 | return {
|
184 | 182 | priority: -1,
|
185 | 183 | restrict: 'A',
|
186 | 184 | compile: function(tElement, tAttrs) {
|
187 | 185 | // gets the expected controller name, converts <data-some-thing> into "someThing"
|
188 | 186 | var controllerName = directiveNormalize(nodeName_(tElement));
|
189 | 187 |
|
190 |
| - // get the symbol name where to set the reference in the scope |
191 |
| - var symbolName = tAttrs.ngRef; |
192 |
| - |
193 |
| - if (symbolName && (!/^[$a-zA-Z_][$a-zA-Z0-9_]*$/.test(symbolName) || |
194 |
| - /^(null|undefined|this|\$parent|\$root|\$id)$/.test(symbolName))) { |
195 |
| - throw ngRefMinErr('badident', 'alias \'{0}\' is invalid --- must be a valid JS identifier which is not a reserved name.', |
196 |
| - symbolName); |
197 |
| - } |
| 188 | + // get the expression for value binding |
| 189 | + var getter = $parse(tAttrs.ngRef); |
| 190 | + var setter = getter.assign; |
198 | 191 |
|
199 | 192 | return function(scope, element) {
|
200 | 193 | // gets the controller of the current component or the current DOM element
|
201 | 194 | var controller = element.data('$' + controllerName + 'Controller');
|
202 | 195 | var value = controller || element[0];
|
203 |
| - scope[symbolName] = value; |
| 196 | + setter(scope, value); |
204 | 197 |
|
205 |
| - // when the element is removed, remove it from the scope assignment (nullify it) |
| 198 | + // when the element is removed, remove it (nullify it) |
206 | 199 | element.on('$destroy', function() {
|
207 | 200 | // only remove it if value has not changed,
|
208 | 201 | // carefully because animations (and other procedures) may duplicate elements
|
209 |
| - if (scope[symbolName] === value) { |
210 |
| - scope[symbolName] = null; |
| 202 | + if (getter(scope) === value) { |
| 203 | + setter(scope, null); |
211 | 204 | }
|
212 | 205 | });
|
213 | 206 | };
|
214 | 207 | }
|
215 | 208 | };
|
216 |
| -}; |
| 209 | +}]; |
0 commit comments