feat(di): add support for optional dependencies
This commit is contained in:
@@ -5,7 +5,7 @@ import {ProtoElementInjector, PreBuiltObjects, DirectiveBinding} from 'angular2/
|
||||
import {Parent, Ancestor} from 'angular2/src/core/annotations/visibility';
|
||||
import {EventEmitter} from 'angular2/src/core/annotations/events';
|
||||
import {onDestroy} from 'angular2/src/core/annotations/annotations';
|
||||
import {Injector, Inject, bind} from 'angular2/di';
|
||||
import {Optional, Injector, Inject, bind} from 'angular2/di';
|
||||
import {View} from 'angular2/src/core/compiler/view';
|
||||
import {ViewContainer} from 'angular2/src/core/compiler/view_container';
|
||||
import {NgElement} from 'angular2/src/core/dom/element';
|
||||
@@ -36,6 +36,13 @@ class NeedsDirective {
|
||||
}
|
||||
}
|
||||
|
||||
class OptionallyNeedsDirective {
|
||||
dependency:SimpleDirective;
|
||||
constructor(@Optional() dependency:SimpleDirective){
|
||||
this.dependency = dependency;
|
||||
}
|
||||
}
|
||||
|
||||
class NeedDirectiveFromParent {
|
||||
dependency:SimpleDirective;
|
||||
constructor(@Parent() dependency:SimpleDirective){
|
||||
@@ -342,6 +349,12 @@ export function main() {
|
||||
toThrowError('No provider for SimpleDirective! (NeedDirectiveFromParent -> SimpleDirective)');
|
||||
});
|
||||
|
||||
it("should inject null when no directive found", function () {
|
||||
var inj = injector([OptionallyNeedsDirective]);
|
||||
var d = inj.get(OptionallyNeedsDirective);
|
||||
expect(d.dependency).toEqual(null);
|
||||
});
|
||||
|
||||
it("should accept SimpleDirective bindings instead of SimpleDirective types", function () {
|
||||
var inj = injector([
|
||||
DirectiveBinding.createFromBinding(bind(SimpleDirective).toClass(SimpleDirective), null)
|
||||
|
||||
Reference in New Issue
Block a user