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

Commit a08a1cf

Browse files
committed
feat(attr-interpolate): add attribute interpolation.
1 parent f1b2cda commit a08a1cf

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/directives/ng_mustache.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ class NgTextMustacheDirective {
44
static String $selector = r':contains(/{{.*}}/)';
55

66
dom.Node element;
7-
DirectiveValue value;
87
ParsedFn interpolateFn;
98

10-
NgTextMustacheDirective(dom.Node this.element, DirectiveValue this.value, Interpolate interpolate) {
9+
NgTextMustacheDirective(dom.Node this.element, DirectiveValue value, Interpolate interpolate) {
1110
interpolateFn = interpolate(value.value);
1211
element.text = '';
1312
}
@@ -19,8 +18,21 @@ class NgTextMustacheDirective {
1918

2019
class NgAttrMustacheDirective {
2120
static String $selector = r'[*=/{{.*}}/]';
21+
static RegExp ATTR_NAME_VALUE_REGEXP = new RegExp(r'^([^=]+)=(.*)$');
2222

23-
attach(Scope scope) {
23+
ParsedFn interpolateFn;
24+
dom.Element element;
25+
Function attrSetter;
2426

27+
NgAttrMustacheDirective(dom.Element this.element, DirectiveValue value, Interpolate interpolate) {
28+
var match = ATTR_NAME_VALUE_REGEXP.firstMatch(value.value);
29+
var attrName = match[1];
30+
interpolateFn = interpolate(match[2]);
31+
attrSetter = (text) => element.attributes[attrName] = text;
32+
attrSetter('');
33+
}
34+
35+
attach(Scope scope) {
36+
scope.$watch(interpolateFn, attrSetter);
2537
}
2638
}

test/_specs.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class JQuery implements List<Node> {
133133
toString() => fold('', (html, node) => '$html${_toHtml(node, true)}');
134134
eq(num childIndex) => $(this[childIndex]);
135135
remove() => forEach((n) => n.remove());
136+
attr([String name]) => accessor((n) => n.attributes[name], (n, v) => n.attributes[name] = v);
136137
}
137138

138139
class Logger implements List {

test/directives/ng_mustache_spec.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,22 @@ main() {
1919
$rootScope.$digest();
2020
expect(element.text()).toEqual('OK!');
2121
}));
22+
23+
24+
it('should replace {{}} in attribute', inject((Compiler $compile, Scope $rootScope) {
25+
var element = $('<div some-attr="{{name}}"></div>');
26+
var template = $compile(element);
27+
28+
$rootScope.name = 'OK';
29+
var block = template();
30+
31+
element = $(block.elements);
32+
33+
block.attach($rootScope);
34+
35+
expect(element.attr('some-attr')).toEqual('');
36+
$rootScope.$digest();
37+
expect(element.attr('some-attr')).toEqual('OK');
38+
}));
2239
});
2340
}

0 commit comments

Comments
 (0)