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

Commit 73e818d

Browse files
drpicoxNarretz
authored andcommitted
feat(ngRef): 2. bind dom element
1 parent 9d350b1 commit 73e818d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/ng/directive/ngRef.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,16 @@ var ngRefDirective = function() {
197197
}
198198

199199
return function(scope, element) {
200-
// gets the controller of the current element (see jqLiteController for details)
200+
// gets the controller of the current component or the current DOM element
201201
var controller = element.data('$' + controllerName + 'Controller');
202-
scope[symbolName] = controller;
202+
var value = controller || element[0];
203+
scope[symbolName] = value;
203204

204205
// when the element is removed, remove it from the scope assignment (nullify it)
205206
element.on('$destroy', function() {
206-
// only remove it if controller has not changed,
207-
// because it can happen that animations (and other procedures) may duplicate elements
208-
if (scope[symbolName] === controller) {
207+
// only remove it if value has not changed,
208+
// carefully because animations (and other procedures) may duplicate elements
209+
if (scope[symbolName] === value) {
209210
scope[symbolName] = null;
210211
}
211212
});

test/ng/directive/ngRefSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ describe('ngRef', function() {
137137

138138
});
139139

140+
it('should bind the dom element if no component', inject(function($compile, $rootScope) {
141+
$compile('<span ng-ref="mySpan">my text</span>')($rootScope);
142+
expect($rootScope.mySpan.textContent).toBe('my text');
143+
}));
144+
145+
it('should nullify the dom element value if it is destroyed', inject(function($compile, $rootScope) {
146+
var element = $compile('<div><span ng-ref="mySpan">my text</span></div>')($rootScope);
147+
element.children().remove();
148+
expect($rootScope.mySpan).toBe(null);
149+
}));
150+
140151
it('should be compatible with directives on entities with controller', function() {
141152
var myDirectiveController;
142153

0 commit comments

Comments
 (0)