parent
f4e0f51f5a
commit
dd3e6271c2
@ -220,7 +220,7 @@ To better understand the kinds of injections which are supported in Angular we h
|
|||||||
|
|
||||||
### Injecting Services
|
### Injecting Services
|
||||||
|
|
||||||
Service injection is the most straight forward kind of injection which Angular supports. It involves a component configuring the `componentServices` and then letting the directive ask for the configured service.
|
Service injection is the most straight forward kind of injection which Angular supports. It involves a component configuring the `services` and then letting the directive ask for the configured service.
|
||||||
|
|
||||||
This example illustrates how to inject `MyService` into `House` directive.
|
This example illustrates how to inject `MyService` into `House` directive.
|
||||||
|
|
||||||
@ -231,7 +231,7 @@ class MyService {} | Assume a service which needs to be inject
|
|||||||
|
|
|
|
||||||
@Component({ | Assume a top level application component which
|
@Component({ | Assume a top level application component which
|
||||||
selector: 'my-app', | configures the services to be injected.
|
selector: 'my-app', | configures the services to be injected.
|
||||||
componentServices: [MyService] |
|
services: [MyService] |
|
||||||
}) |
|
}) |
|
||||||
@Template({ | Assume we have a template that needs to be
|
@Template({ | Assume we have a template that needs to be
|
||||||
url: 'my_app.html', | configured with directives to be injected.
|
url: 'my_app.html', | configured with directives to be injected.
|
||||||
|
@ -5,7 +5,6 @@ import {ListWrapper, List} from 'angular2/src/facade/collection';
|
|||||||
export class Directive {
|
export class Directive {
|
||||||
selector:any; //string;
|
selector:any; //string;
|
||||||
bind:any;
|
bind:any;
|
||||||
lightDomServices:any; //List;
|
|
||||||
implementsTypes:any; //List;
|
implementsTypes:any; //List;
|
||||||
lifecycle:any; //List
|
lifecycle:any; //List
|
||||||
events:any; //List
|
events:any; //List
|
||||||
@ -14,20 +13,17 @@ export class Directive {
|
|||||||
selector,
|
selector,
|
||||||
bind,
|
bind,
|
||||||
events,
|
events,
|
||||||
lightDomServices,
|
|
||||||
implementsTypes,
|
implementsTypes,
|
||||||
lifecycle
|
lifecycle
|
||||||
}:{
|
}:{
|
||||||
selector:string,
|
selector:string,
|
||||||
bind:any,
|
bind:any,
|
||||||
events: any,
|
events: any,
|
||||||
lightDomServices:List,
|
|
||||||
implementsTypes:List,
|
implementsTypes:List,
|
||||||
lifecycle:List
|
lifecycle:List
|
||||||
}={})
|
}={})
|
||||||
{
|
{
|
||||||
this.selector = selector;
|
this.selector = selector;
|
||||||
this.lightDomServices = lightDomServices;
|
|
||||||
this.implementsTypes = implementsTypes;
|
this.implementsTypes = implementsTypes;
|
||||||
this.bind = bind;
|
this.bind = bind;
|
||||||
this.events = events;
|
this.events = events;
|
||||||
@ -41,26 +37,21 @@ export class Directive {
|
|||||||
|
|
||||||
export class Component extends Directive {
|
export class Component extends Directive {
|
||||||
//TODO: vsavkin: uncomment it once the issue with defining fields in a sublass works
|
//TODO: vsavkin: uncomment it once the issue with defining fields in a sublass works
|
||||||
shadowDomServices:any; //List;
|
services:any; //List;
|
||||||
componentServices:any; //List;
|
|
||||||
|
|
||||||
@CONST()
|
@CONST()
|
||||||
constructor({
|
constructor({
|
||||||
selector,
|
selector,
|
||||||
bind,
|
bind,
|
||||||
events,
|
events,
|
||||||
lightDomServices,
|
services,
|
||||||
shadowDomServices,
|
|
||||||
componentServices,
|
|
||||||
implementsTypes,
|
implementsTypes,
|
||||||
lifecycle
|
lifecycle
|
||||||
}:{
|
}:{
|
||||||
selector:String,
|
selector:String,
|
||||||
bind:Object,
|
bind:Object,
|
||||||
events:Object,
|
events:Object,
|
||||||
lightDomServices:List,
|
services:List,
|
||||||
shadowDomServices:List,
|
|
||||||
componentServices:List,
|
|
||||||
implementsTypes:List,
|
implementsTypes:List,
|
||||||
lifecycle:List
|
lifecycle:List
|
||||||
}={})
|
}={})
|
||||||
@ -69,13 +60,11 @@ export class Component extends Directive {
|
|||||||
selector: selector,
|
selector: selector,
|
||||||
bind: bind,
|
bind: bind,
|
||||||
events: events,
|
events: events,
|
||||||
lightDomServices: lightDomServices,
|
|
||||||
implementsTypes: implementsTypes,
|
implementsTypes: implementsTypes,
|
||||||
lifecycle: lifecycle
|
lifecycle: lifecycle
|
||||||
});
|
});
|
||||||
|
|
||||||
this.shadowDomServices = shadowDomServices;
|
this.services = services;
|
||||||
this.componentServices = componentServices;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +75,6 @@ export class Decorator extends Directive {
|
|||||||
selector,
|
selector,
|
||||||
bind,
|
bind,
|
||||||
events,
|
events,
|
||||||
lightDomServices,
|
|
||||||
implementsTypes,
|
implementsTypes,
|
||||||
lifecycle,
|
lifecycle,
|
||||||
compileChildren = true,
|
compileChildren = true,
|
||||||
@ -94,7 +82,6 @@ export class Decorator extends Directive {
|
|||||||
selector:string,
|
selector:string,
|
||||||
bind:any,
|
bind:any,
|
||||||
events:any,
|
events:any,
|
||||||
lightDomServices:List,
|
|
||||||
implementsTypes:List,
|
implementsTypes:List,
|
||||||
lifecycle:List,
|
lifecycle:List,
|
||||||
compileChildren:boolean
|
compileChildren:boolean
|
||||||
@ -105,7 +92,6 @@ export class Decorator extends Directive {
|
|||||||
selector: selector,
|
selector: selector,
|
||||||
bind: bind,
|
bind: bind,
|
||||||
events: events,
|
events: events,
|
||||||
lightDomServices: lightDomServices,
|
|
||||||
implementsTypes: implementsTypes,
|
implementsTypes: implementsTypes,
|
||||||
lifecycle: lifecycle
|
lifecycle: lifecycle
|
||||||
});
|
});
|
||||||
@ -118,13 +104,11 @@ export class Viewport extends Directive {
|
|||||||
selector,
|
selector,
|
||||||
bind,
|
bind,
|
||||||
events,
|
events,
|
||||||
lightDomServices,
|
|
||||||
implementsTypes,
|
implementsTypes,
|
||||||
lifecycle
|
lifecycle
|
||||||
}:{
|
}:{
|
||||||
selector:string,
|
selector:string,
|
||||||
bind:any,
|
bind:any,
|
||||||
lightDomServices:List,
|
|
||||||
implementsTypes:List,
|
implementsTypes:List,
|
||||||
lifecycle:List
|
lifecycle:List
|
||||||
}={})
|
}={})
|
||||||
@ -133,7 +117,6 @@ export class Viewport extends Directive {
|
|||||||
selector: selector,
|
selector: selector,
|
||||||
bind: bind,
|
bind: bind,
|
||||||
events: events,
|
events: events,
|
||||||
lightDomServices: lightDomServices,
|
|
||||||
implementsTypes: implementsTypes,
|
implementsTypes: implementsTypes,
|
||||||
lifecycle: lifecycle
|
lifecycle: lifecycle
|
||||||
});
|
});
|
||||||
|
4
modules/angular2/src/core/application.js
vendored
4
modules/angular2/src/core/application.js
vendored
@ -43,9 +43,7 @@ function _injectorBindings(appComponentType): List<Binding> {
|
|||||||
return [
|
return [
|
||||||
bind(appDocumentToken).toValue(DOM.defaultDoc()),
|
bind(appDocumentToken).toValue(DOM.defaultDoc()),
|
||||||
bind(appComponentAnnotatedTypeToken).toFactory((reader) => {
|
bind(appComponentAnnotatedTypeToken).toFactory((reader) => {
|
||||||
// TODO(rado): inspect annotation here and warn if there are bindings,
|
// TODO(rado): investigate whether to support bindings on root component.
|
||||||
// lightDomServices, and other component annotations that are skipped
|
|
||||||
// for bootstrapping components.
|
|
||||||
return reader.read(appComponentType);
|
return reader.read(appComponentType);
|
||||||
}, [DirectiveMetadataReader]),
|
}, [DirectiveMetadataReader]),
|
||||||
|
|
||||||
|
@ -34,9 +34,6 @@ export class ProtoElementInjectorBuilder extends CompileStep {
|
|||||||
var distanceToParentInjector = this._getDistanceToParentInjector(parent, current);
|
var distanceToParentInjector = this._getDistanceToParentInjector(parent, current);
|
||||||
var parentProtoElementInjector = this._getParentProtoElementInjector(parent, current);
|
var parentProtoElementInjector = this._getParentProtoElementInjector(parent, current);
|
||||||
var injectorBindings = ListWrapper.map(current.getAllDirectives(), this._createBinding);
|
var injectorBindings = ListWrapper.map(current.getAllDirectives(), this._createBinding);
|
||||||
// TODO: add lightDomServices as well,
|
|
||||||
// but after the directives as we rely on that order
|
|
||||||
// in the element_binder_builder.
|
|
||||||
|
|
||||||
// Create a protoElementInjector for any element that either has bindings *or* has one
|
// Create a protoElementInjector for any element that either has bindings *or* has one
|
||||||
// or more var- defined. Elements with a var- defined need a their own element injector
|
// or more var- defined. Elements with a var- defined need a their own element injector
|
||||||
|
2
modules/angular2/src/core/compiler/view.js
vendored
2
modules/angular2/src/core/compiler/view.js
vendored
@ -144,7 +144,7 @@ export class View {
|
|||||||
|
|
||||||
// shadowDomAppInjector
|
// shadowDomAppInjector
|
||||||
if (isPresent(componentDirective)) {
|
if (isPresent(componentDirective)) {
|
||||||
var services = componentDirective.annotation.componentServices;
|
var services = componentDirective.annotation.services;
|
||||||
if (isPresent(services))
|
if (isPresent(services))
|
||||||
shadowDomAppInjector = appInjector.createChild(services);
|
shadowDomAppInjector = appInjector.createChild(services);
|
||||||
else {
|
else {
|
||||||
|
@ -585,7 +585,7 @@ class ComponentWithPipes {
|
|||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'child-cmp',
|
selector: 'child-cmp',
|
||||||
componentServices: [MyService]
|
services: [MyService]
|
||||||
})
|
})
|
||||||
@Template({
|
@Template({
|
||||||
directives: [MyDir],
|
directives: [MyDir],
|
||||||
@ -635,7 +635,7 @@ class CompWithAncestor {
|
|||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: '[child-cmp2]',
|
selector: '[child-cmp2]',
|
||||||
componentServices: [MyService]
|
services: [MyService]
|
||||||
})
|
})
|
||||||
class ChildComp2 {
|
class ChildComp2 {
|
||||||
ctxProp:string;
|
ctxProp:string;
|
||||||
|
@ -209,7 +209,7 @@ class SomeComponentService {}
|
|||||||
class SomeViewportDirective {}
|
class SomeViewportDirective {}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
componentServices: [SomeComponentService]
|
services: [SomeComponentService]
|
||||||
})
|
})
|
||||||
class SomeComponentDirective {}
|
class SomeComponentDirective {}
|
||||||
|
|
||||||
|
@ -676,7 +676,7 @@ class DirectiveImplementingOnChange {
|
|||||||
|
|
||||||
class SomeService {}
|
class SomeService {}
|
||||||
|
|
||||||
@Component({componentServices: [SomeService]})
|
@Component({services: [SomeService]})
|
||||||
class SomeComponent {
|
class SomeComponent {
|
||||||
service: SomeService;
|
service: SomeService;
|
||||||
constructor(service: SomeService) {
|
constructor(service: SomeService) {
|
||||||
|
@ -2,7 +2,7 @@ library bar;
|
|||||||
|
|
||||||
import 'package:angular2/src/core/annotations/annotations.dart';
|
import 'package:angular2/src/core/annotations/annotations.dart';
|
||||||
|
|
||||||
@Component(selector: 'soup', componentServices: const [ToolTip])
|
@Component(selector: 'soup', services: const [ToolTip])
|
||||||
class MyComponent {}
|
class MyComponent {}
|
||||||
|
|
||||||
@Decorator(selector: '[tool-tip]', bind: const {'text': 'tool-tip'})
|
@Decorator(selector: '[tool-tip]', bind: const {'text': 'tool-tip'})
|
||||||
|
@ -12,7 +12,7 @@ void setupReflection(reflector) {
|
|||||||
"factory": () => new MyComponent(),
|
"factory": () => new MyComponent(),
|
||||||
"parameters": const [],
|
"parameters": const [],
|
||||||
"annotations": const [
|
"annotations": const [
|
||||||
const Component(selector: 'soup', componentServices: const [ToolTip])
|
const Component(selector: 'soup', services: const [ToolTip])
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
..registerType(ToolTip, {
|
..registerType(ToolTip, {
|
||||||
|
@ -4,7 +4,7 @@ import 'package:angular2/src/core/annotations/annotations.dart';
|
|||||||
import 'foo.dart' as dep;
|
import 'foo.dart' as dep;
|
||||||
|
|
||||||
@Component(
|
@Component(
|
||||||
selector: '[soup]', componentServices: const [dep.DependencyComponent])
|
selector: '[soup]', services: const [dep.DependencyComponent])
|
||||||
class MyComponent {
|
class MyComponent {
|
||||||
MyComponent();
|
MyComponent();
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ void setupReflection(reflector) {
|
|||||||
"annotations": const [
|
"annotations": const [
|
||||||
const Component(
|
const Component(
|
||||||
selector: '[soup]',
|
selector: '[soup]',
|
||||||
componentServices: const [dep.DependencyComponent])
|
services: const [dep.DependencyComponent])
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
i0.setupReflection(reflector);
|
i0.setupReflection(reflector);
|
||||||
|
@ -3,7 +3,7 @@ library bar;
|
|||||||
import 'package:angular2/src/core/annotations/annotations.dart';
|
import 'package:angular2/src/core/annotations/annotations.dart';
|
||||||
import 'foo.dart';
|
import 'foo.dart';
|
||||||
|
|
||||||
@Component(componentServices: const [MyContext])
|
@Component(services: const [MyContext])
|
||||||
class MyComponent {
|
class MyComponent {
|
||||||
final MyContext c;
|
final MyContext c;
|
||||||
MyComponent(this.c);
|
MyComponent(this.c);
|
||||||
|
@ -13,7 +13,7 @@ void setupReflection(reflector) {
|
|||||||
"factory": (MyContext c) => new MyComponent(c),
|
"factory": (MyContext c) => new MyComponent(c),
|
||||||
"parameters": const [const [MyContext]],
|
"parameters": const [const [MyContext]],
|
||||||
"annotations": const [
|
"annotations": const [
|
||||||
const Component(componentServices: const [MyContext])
|
const Component(services: const [MyContext])
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import {bootstrap, Component, Decorator, Template, NgElement} from 'angular2/ang
|
|||||||
selector: 'hello-app',
|
selector: 'hello-app',
|
||||||
// These are services that would be created if a class in the component's
|
// These are services that would be created if a class in the component's
|
||||||
// template tries to inject them.
|
// template tries to inject them.
|
||||||
componentServices: [GreetingService]
|
services: [GreetingService]
|
||||||
})
|
})
|
||||||
// The template for the component.
|
// The template for the component.
|
||||||
@Template({
|
@Template({
|
||||||
|
@ -29,7 +29,7 @@ function setup() {
|
|||||||
"annotations" : [
|
"annotations" : [
|
||||||
new Component({
|
new Component({
|
||||||
selector: 'hello-app',
|
selector: 'hello-app',
|
||||||
componentServices: [app.GreetingService]
|
services: [app.GreetingService]
|
||||||
}),
|
}),
|
||||||
new Template({
|
new Template({
|
||||||
directives: [app.RedDec],
|
directives: [app.RedDec],
|
||||||
|
@ -22,7 +22,6 @@ function namedObjectType({a,b}:{a:A,b:B<C>}) {
|
|||||||
class Bar {
|
class Bar {
|
||||||
constructor({
|
constructor({
|
||||||
selector,
|
selector,
|
||||||
lightDomServices,
|
|
||||||
implementsTypes
|
implementsTypes
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user