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

Commit 1f1afd0

Browse files
committed
feat: add ng-controller directive
1 parent 2b1f506 commit 1f1afd0

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

lib/angular.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ part 'block_type.dart';
1313
part 'compiler.dart';
1414
part 'directive.dart';
1515
part 'directives/ng_bind.dart';
16+
part 'directives/ng_controller.dart';
1617
part 'directives/ng_mustache.dart';
1718
part 'directives/ng_repeat.dart';
1819
part 'dom_utilities.dart';
@@ -123,4 +124,5 @@ angularModule(AngularModule module) {
123124

124125
module.directive(NgBindAttrDirective);
125126
module.directive(NgRepeatAttrDirective);
127+
module.directive(NgControllerAttrDirective);
126128
}

lib/directives/ng_controller.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
part of angular;
2+
3+
class NgControllerAttrDirective {
4+
static String $transclude = 'element';
5+
static RegExp CTRL_REGEXP = new RegExp(r'^(\S+)(\s+as\s+(\w+))?$');
6+
7+
Symbol ctrlSymbol;
8+
String alias;
9+
Injector injector;
10+
BlockList blockList;
11+
12+
NgControllerAttrDirective(DirectiveValue value, Injector this.injector, BlockList this.blockList) {
13+
var match = CTRL_REGEXP.firstMatch(value.value);
14+
15+
ctrlSymbol = new Symbol(match.group(1) + 'Controller');
16+
alias = match.group(3);
17+
}
18+
19+
attach(Scope scope) {
20+
var childScope = scope.$new();
21+
var module = new Module();
22+
module.value(Scope, childScope);
23+
24+
// attach the child scope
25+
blockList.newBlock()..attach(childScope)..insertAfter(blockList);
26+
27+
// instantiate the controller
28+
var controller = injector.createChild([module], [ctrlSymbol]).getBySymbol(ctrlSymbol);
29+
30+
// publish the controller into the scope
31+
if (alias != null) {
32+
childScope[alias] = controller;
33+
}
34+
}
35+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import '../_specs.dart';
2+
3+
4+
class MainController {
5+
Scope _scope;
6+
String name = 'name on controller';
7+
8+
MainController(Scope this._scope) {
9+
_scope['name'] = 'Vojta';
10+
}
11+
}
12+
13+
14+
main() {
15+
beforeEach(module(angularModule));
16+
17+
describe('NgController', () {
18+
var compile, element, rootScope;
19+
20+
beforeEach(inject((Scope scope, Compiler compiler) {
21+
compile = (html, [applyFn]) {
22+
element = $(html);
23+
rootScope = scope;
24+
compiler(element)(element).attach(scope);
25+
scope.$apply(applyFn);
26+
};
27+
}));
28+
29+
30+
it('should instantiate controller', () {
31+
compile('<div><div ng-controller="Main" class="controller">Hi {{name}}</div></div>');
32+
expect(element.find('.controller').text()).toEqual('Hi Vojta');
33+
});
34+
35+
36+
it('should create a new scope', () {
37+
compile('<div><div ng-controller="Main" class="controller">Hi {{name}}</div><div class="siblink">{{name}}</div></div>', () {
38+
rootScope['name'] = 'parent';
39+
});
40+
41+
expect(element.find('.controller').text()).toEqual('Hi Vojta');
42+
expect(element.find('.siblink').text()).toEqual('parent');
43+
});
44+
45+
46+
it('should export controller', () {
47+
compile('<div><div ng-controller="Main as main" class="controller">Hi {{main.name}}</div></div>');
48+
expect(element.find('.controller').text()).toEqual('Hi name on controller');
49+
});
50+
});
51+
}

0 commit comments

Comments
 (0)