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

Commit e285f06

Browse files
committed
feat: add ng-if directive
This is very initial version, that only removes the element (block) from the DOM. It does not detach the scope and therefore it is still digested. Misko is changing this completely (removing atach/detach API), so I will implement this once Misko is done.
1 parent d0f200e commit e285f06

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

lib/angular.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ part 'directives/ng_click.dart';
1919
part 'directives/ng_cloak.dart';
2020
part 'directives/ng_controller.dart';
2121
part 'directives/ng_hide.dart';
22+
part 'directives/ng_if.dart';
2223
part 'directives/ng_mustache.dart';
2324
part 'directives/ng_repeat.dart';
2425
part 'directives/ng_show.dart';
@@ -143,6 +144,7 @@ angularModule(AngularModule module) {
143144
module.directive(NgCloakAttrDirective);
144145
module.directive(NgControllerAttrDirective);
145146
module.directive(NgHideAttrDirective);
147+
module.directive(NgIfAttrDirective);
146148
module.directive(NgRepeatAttrDirective);
147149
module.directive(NgShowAttrDirective);
148150
}

lib/directives/ng_if.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
part of angular;
2+
3+
4+
class NgIfAttrDirective {
5+
static String $transclude = 'element';
6+
7+
String expression;
8+
dom.Element node;
9+
Injector injector;
10+
BlockList blockList;
11+
12+
NgIfAttrDirective(DirectiveValue value, Injector this.injector, BlockList this.blockList, dom.Node this.node) {
13+
expression = value.value;
14+
}
15+
16+
attach(Scope scope) {
17+
// TODO(vojta): detach the scope when block is removed
18+
var childScope = scope.$new();
19+
var block = blockList.newBlock();
20+
var isInserted = false;
21+
22+
block.attach(childScope);
23+
24+
scope.$watch(expression, (value, _, __) {
25+
// TODO(vojta): ignore changes like null -> false
26+
if (value != null && toBool(value)) {
27+
if (!isInserted) {
28+
block.insertAfter(blockList);
29+
isInserted = true;
30+
}
31+
} else {
32+
if (isInserted) {
33+
block.remove();
34+
isInserted = false;
35+
}
36+
}
37+
});
38+
}
39+
}

test/directives/ng_if_spec.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import '../_specs.dart';
2+
import 'dart:html' as dom;
3+
4+
5+
main() {
6+
beforeEach(module(angularModule));
7+
8+
describe('NgIf', () {
9+
var compile, element, rootScope;
10+
11+
beforeEach(inject((Scope scope, Compiler compiler) {
12+
compile = (html, [applyFn]) {
13+
element = $(html);
14+
rootScope = scope;
15+
compiler(element)(element).attach(scope);
16+
scope.$apply(applyFn);
17+
};
18+
}));
19+
20+
21+
it('should add/remove the element', () {
22+
compile('<div><span ng-if="isVisible">content</span></div>');
23+
24+
expect(element.find('span').html()).toEqual('');
25+
26+
rootScope.$apply(() {
27+
rootScope['isVisible'] = true;
28+
});
29+
expect(element.find('span').html()).toEqual('content');
30+
31+
rootScope.$apply(() {
32+
rootScope['isVisible'] = false;
33+
});
34+
expect(element.find('span').html()).toEqual('');
35+
});
36+
});
37+
}

0 commit comments

Comments
 (0)