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

Commit 5fdab52

Browse files
committed
feat(jqLite): make injector() and scope() work with the document object
For typical app that has ng-app directive on the html element, we now can do: angular.element(document).injector() or .injector() angular.element(document).scope() or .scope() instead of: angular.element(document.getElementsByTagName('html')[0]).injector() ...
1 parent 541bedd commit 5fdab52

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/jqLite.js

+7
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,13 @@ function JQLiteController(element, name) {
279279

280280
function JQLiteInheritedData(element, name, value) {
281281
element = jqLite(element);
282+
283+
// if element is the document object work with the html element instead
284+
// this makes $(document).scope() possible
285+
if(element[0].nodeType == 9) {
286+
element = element.find('html');
287+
}
288+
282289
while (element.length) {
283290
if (value = element.data(name)) return value;
284291
element = element.parent();

test/jqLiteSpec.js

+47
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ describe('jqLite', function() {
107107
expect(deepChild.inheritedData('myData')).toBeFalsy();
108108
dealoc(element);
109109
});
110+
111+
112+
it('should work with the child html element instead if the current element is the document obj',
113+
function() {
114+
var item = {},
115+
doc = jqLite(document),
116+
html = doc.find('html');
117+
118+
html.data('item', item);
119+
expect(doc.inheritedData('item')).toBe(item);
120+
expect(html.inheritedData('item')).toBe(item);
121+
dealoc(doc);
122+
}
123+
);
110124
});
111125

112126

@@ -118,6 +132,18 @@ describe('jqLite', function() {
118132
dealoc(element);
119133
});
120134

135+
it('should retrieve scope attached to the html element if its requested on the document',
136+
function() {
137+
var doc = jqLite(document),
138+
html = doc.find('html'),
139+
scope = {};
140+
141+
html.data('$scope', scope);
142+
143+
expect(doc.scope()).toBe(scope);
144+
expect(html.scope()).toBe(scope);
145+
dealoc(doc);
146+
});
121147

122148
it('should walk up the dom to find scope', function() {
123149
var element = jqLite('<ul><li><p><b>deep deep</b><p></li></ul>');
@@ -147,6 +173,27 @@ describe('jqLite', function() {
147173
expect(span.injector()).toBe(injector);
148174
dealoc(template);
149175
});
176+
177+
178+
it('should retrieve injector attached to the html element if its requested on document',
179+
function() {
180+
var doc = jqLite(document),
181+
html = doc.find('html'),
182+
injector = {};
183+
184+
html.data('$injector', injector);
185+
186+
expect(doc.injector()).toBe(injector);
187+
expect(html.injector()).toBe(injector);
188+
dealoc(doc);
189+
});
190+
191+
192+
it('should do nothing with a noncompiled template', function() {
193+
var template = jqLite('<div><span></span></div>');
194+
expect(template.injector()).toBeUndefined();
195+
dealoc(template);
196+
});
150197
});
151198

152199

0 commit comments

Comments
 (0)