Skip to content

Commit a3387b7

Browse files
fix(di): allow injecting static attrs without type annotations
Closes angular#1226
1 parent 94a48e8 commit a3387b7

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

modules/angular2/src/core/annotations/di.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ export class Attribute extends DependencyAnnotation {
4545
super();
4646
this.attributeName = attributeName;
4747
}
48+
49+
get token() {
50+
//Normally one would default a token to a type of an injected value but here
51+
//the type of a variable is "string" and we can't use primitive type as a return value
52+
//so we use instance of Attribute instead. This doesn't matter much in practice as arguments
53+
//with @Attribute annotation are injected by ElementInjector that doesn't take tokens into account.
54+
return this;
55+
}
4856
}

modules/angular2/src/di/binding.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ function _extractToken(typeOrFunc, annotations) {
140140
}
141141
ListWrapper.push(depProps, paramAnnotation);
142142

143-
} else if (paramAnnotation.name === "string") {
144-
token = paramAnnotation;
145143
}
146144
}
147145

modules/angular2/test/core/compiler/element_injector_spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ class NeedsAttribute {
145145
}
146146
}
147147

148+
class NeedsAttributeNoType {
149+
fooAttribute;
150+
constructor(@Attribute('foo') fooAttribute) {
151+
this.fooAttribute = fooAttribute;
152+
}
153+
}
154+
148155
class A_Needs_B {
149156
constructor(dep){}
150157
}
@@ -639,7 +646,7 @@ export function main() {
639646
});
640647
});
641648

642-
describe('static', () => {
649+
describe('static attributes', () => {
643650
it('should be injectable', () => {
644651
var attributes = MapWrapper.create();
645652
MapWrapper.set(attributes, 'type', 'text');
@@ -652,6 +659,16 @@ export function main() {
652659
expect(needsAttribute.titleAttribute).toEqual('');
653660
expect(needsAttribute.fooAttribute).toEqual(null);
654661
});
662+
663+
it('should be injectable without type annotation', () => {
664+
var attributes = MapWrapper.create();
665+
MapWrapper.set(attributes, 'foo', 'bar');
666+
667+
var inj = injector([NeedsAttributeNoType], null, null, null, attributes);
668+
var needsAttribute = inj.get(NeedsAttributeNoType);
669+
670+
expect(needsAttribute.fooAttribute).toEqual('bar');
671+
});
655672
});
656673

657674
});

0 commit comments

Comments
 (0)