Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 71f8bb5

Browse files
committed
feat(ng-include): basic implementation of ng-include directive
1 parent ec3b0e5 commit 71f8bb5

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

lib/angular.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ part 'directives/ng_controller.dart';
2222
part 'directives/ng_disabled.dart';
2323
part 'directives/ng_hide.dart';
2424
part 'directives/ng_if.dart';
25+
part 'directives/ng_include.dart';
2526
part 'directives/ng_model.dart';
2627
part 'directives/ng_mustache.dart';
2728
part 'directives/ng_repeat.dart';
@@ -147,6 +148,7 @@ class AngularModule extends Module {
147148
directive(NgDisabledAttrDirective);
148149
directive(NgHideAttrDirective);
149150
directive(NgIfAttrDirective);
151+
directive(NgIncludeAttrDirective);
150152
directive(NgRepeatAttrDirective);
151153
directive(NgShowAttrDirective);
152154

lib/directives/ng_include.dart

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
part of angular;
2+
3+
class NgIncludeAttrDirective {
4+
5+
NgIncludeAttrDirective(dom.Element element, Scope scope, NodeAttrs attrs, BlockCache blockCache, Injector injector) {
6+
7+
var previousBlock = null;
8+
var previousScope = null;
9+
10+
cleanUp() {
11+
if (previousBlock == null) {
12+
return;
13+
}
14+
15+
previousBlock.remove();
16+
previousScope.$destroy();
17+
element.innerHtml = '';
18+
19+
previousBlock = null;
20+
previousScope = null;
21+
};
22+
23+
updateContent(createBlock) {
24+
cleanUp();
25+
26+
// create a new scope
27+
previousScope = scope.$new();
28+
previousBlock = createBlock(injector.createChild([new ScopeModule(previousScope)]));
29+
30+
previousBlock.elements.forEach((elm) {
31+
element.append(elm);
32+
});
33+
};
34+
35+
scope.$watch(attrs[this], (value, another) {
36+
print('ng-include tpl changed to $value');
37+
38+
if (value == null || value == '') {
39+
cleanUp();
40+
return;
41+
}
42+
43+
if (value.startsWith('<')) {
44+
// inlined template
45+
updateContent(blockCache.fromHtml(value));
46+
} else {
47+
// an url template
48+
blockCache.fromUrl(value).then((createBlock) {
49+
updateContent(createBlock);
50+
51+
// Http should take care of this
52+
scope.$digest();
53+
});
54+
}
55+
});
56+
}
57+
}

test/directives/ng_include_spec.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import '../_specs.dart';
2+
import '../_test_bed.dart';
3+
import '../_http.dart';
4+
5+
main() {
6+
describe('NgInclude', () {
7+
TestBed _;
8+
9+
beforeEach(beforeEachTestBed((tb) => _ = tb));
10+
11+
beforeEach(module((AngularModule module) {
12+
module.type(HttpFutures, MockHttpFutures);
13+
}));
14+
15+
it('should fail', inject((Scope scope, TemplateCache cache, HttpFutures futures) {
16+
cache.put('tpl.html', 'my name is {{name}}');
17+
18+
var element = _.compile('<div ng-include="template"></div>');
19+
20+
expect(element.html()).toEqual('');
21+
22+
scope.$apply(() {
23+
scope['name'] = 'Vojta';
24+
scope['template'] = 'tpl.html';
25+
});
26+
futures.trigger();
27+
expect(element.text()).toEqual('my name is Vojta');
28+
}));
29+
30+
31+
it('should support inlined templates', inject((Scope scope) {
32+
var element = _.compile('<div ng-include="template"></div>');
33+
34+
scope.$apply(() {
35+
scope['name'] = 'Vojta';
36+
scope['template'] = '<span>my inlined name is {{name}}</span>';
37+
});
38+
expect(element.text()).toEqual('my inlined name is Vojta');
39+
}));
40+
});
41+
}

0 commit comments

Comments
 (0)