refactor: move angular source to /packages rather than modules/@angular
This commit is contained in:
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// #docregion Component
|
||||
import {Component, ContentChild, Directive, Input} from '@angular/core';
|
||||
|
||||
@Directive({selector: 'pane'})
|
||||
export class Pane {
|
||||
@Input() id: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'tab',
|
||||
template: `
|
||||
<div>pane: {{pane?.id}}</div>
|
||||
`
|
||||
})
|
||||
export class Tab {
|
||||
@ContentChild(Pane) pane: Pane;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'example-app',
|
||||
template: `
|
||||
<tab>
|
||||
<pane id="1" *ngIf="shouldShow"></pane>
|
||||
<pane id="2" *ngIf="!shouldShow"></pane>
|
||||
</tab>
|
||||
|
||||
<button (click)="toggle()">Toggle</button>
|
||||
`,
|
||||
})
|
||||
export class ContentChildComp {
|
||||
shouldShow = true;
|
||||
|
||||
toggle() { this.shouldShow = !this.shouldShow; }
|
||||
}
|
||||
// #enddocregion
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// #docregion HowTo
|
||||
import {AfterContentInit, ContentChild, Directive} from '@angular/core';
|
||||
|
||||
@Directive({selector: 'child-directive'})
|
||||
class ChildDirective {
|
||||
}
|
||||
|
||||
@Directive({selector: 'someDir'})
|
||||
class SomeDir implements AfterContentInit {
|
||||
@ContentChild(ChildDirective) contentChild: ChildDirective;
|
||||
|
||||
ngAfterContentInit() {
|
||||
// contentChild is set
|
||||
}
|
||||
}
|
||||
// #enddocregion
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ElementFinder, browser, by, element} from 'protractor';
|
||||
import {verifyNoBrowserErrors} from '../../../../../_common/e2e_util';
|
||||
|
||||
describe('contentChild example', () => {
|
||||
afterEach(verifyNoBrowserErrors);
|
||||
let button: ElementFinder;
|
||||
let result: ElementFinder;
|
||||
|
||||
beforeEach(() => {
|
||||
browser.get('/core/di/ts/contentChild/index.html');
|
||||
button = element(by.css('button'));
|
||||
result = element(by.css('div'));
|
||||
});
|
||||
|
||||
it('should query content child', () => {
|
||||
expect(result.getText()).toEqual('pane: 1');
|
||||
|
||||
button.click();
|
||||
|
||||
expect(result.getText()).toEqual('pane: 2');
|
||||
});
|
||||
});
|
19
packages/examples/core/di/ts/contentChild/module.ts
Normal file
19
packages/examples/core/di/ts/contentChild/module.ts
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {ContentChildComp, Pane, Tab} from './content_child_example';
|
||||
|
||||
@NgModule({
|
||||
imports: [BrowserModule],
|
||||
declarations: [ContentChildComp, Pane, Tab],
|
||||
bootstrap: [ContentChildComp]
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// #docregion Component
|
||||
import {Component, ContentChildren, Directive, Input, QueryList} from '@angular/core';
|
||||
|
||||
@Directive({selector: 'pane'})
|
||||
export class Pane {
|
||||
@Input() id: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'tab',
|
||||
template: `
|
||||
<div>panes: {{serializedPanes}}</div>
|
||||
`
|
||||
})
|
||||
export class Tab {
|
||||
@ContentChildren(Pane) panes: QueryList<Pane>;
|
||||
|
||||
get serializedPanes(): string { return this.panes ? this.panes.map(p => p.id).join(', ') : ''; }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'example-app',
|
||||
template: `
|
||||
<tab>
|
||||
<pane id="1"></pane>
|
||||
<pane id="2"></pane>
|
||||
<pane id="3" *ngIf="shouldShow"></pane>
|
||||
</tab>
|
||||
|
||||
<button (click)="show()">Show 3</button>
|
||||
`,
|
||||
})
|
||||
export class ContentChildrenComp {
|
||||
shouldShow = false;
|
||||
|
||||
show() { this.shouldShow = true; }
|
||||
}
|
||||
// #enddocregion
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// #docregion HowTo
|
||||
import {AfterContentInit, ContentChildren, Directive, QueryList} from '@angular/core';
|
||||
|
||||
@Directive({selector: 'child-directive'})
|
||||
class ChildDirective {
|
||||
}
|
||||
|
||||
@Directive({selector: 'someDir'})
|
||||
class SomeDir implements AfterContentInit {
|
||||
@ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
|
||||
|
||||
ngAfterContentInit() {
|
||||
// contentChildren is set
|
||||
}
|
||||
}
|
||||
// #enddocregion
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ElementFinder, browser, by, element} from 'protractor';
|
||||
import {verifyNoBrowserErrors} from '../../../../../_common/e2e_util';
|
||||
|
||||
describe('contentChildren example', () => {
|
||||
afterEach(verifyNoBrowserErrors);
|
||||
let button: ElementFinder;
|
||||
let result: ElementFinder;
|
||||
|
||||
beforeEach(() => {
|
||||
browser.get('/core/di/ts/contentChildren/index.html');
|
||||
button = element(by.css('button'));
|
||||
result = element(by.css('div'));
|
||||
});
|
||||
|
||||
it('should query content children', () => {
|
||||
expect(result.getText()).toEqual('panes: 1, 2');
|
||||
|
||||
button.click();
|
||||
|
||||
expect(result.getText()).toEqual('panes: 1, 2, 3');
|
||||
});
|
||||
});
|
19
packages/examples/core/di/ts/contentChildren/module.ts
Normal file
19
packages/examples/core/di/ts/contentChildren/module.ts
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {ContentChildrenComp, Pane, Tab} from './content_children_example';
|
||||
|
||||
@NgModule({
|
||||
imports: [BrowserModule],
|
||||
declarations: [ContentChildrenComp, Pane, Tab],
|
||||
bootstrap: [ContentChildrenComp]
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
50
packages/examples/core/di/ts/forward_ref/forward_ref_spec.ts
Normal file
50
packages/examples/core/di/ts/forward_ref/forward_ref_spec.ts
Normal file
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Inject, ReflectiveInjector, forwardRef, resolveForwardRef} from '@angular/core';
|
||||
|
||||
export function main() {
|
||||
describe('forwardRef examples', () => {
|
||||
it('ForwardRefFn example works', () => {
|
||||
// #docregion forward_ref_fn
|
||||
const ref = forwardRef(() => Lock);
|
||||
// #enddocregion
|
||||
expect(ref).not.toBeNull();
|
||||
|
||||
class Lock {}
|
||||
});
|
||||
|
||||
it('can be used to inject a class defined later', () => {
|
||||
// #docregion forward_ref
|
||||
class Door {
|
||||
lock: Lock;
|
||||
|
||||
// Door attempts to inject Lock, despite it not being defined yet.
|
||||
// forwardRef makes this possible.
|
||||
constructor(@Inject(forwardRef(() => Lock)) lock: Lock) { this.lock = lock; }
|
||||
}
|
||||
|
||||
// Only at this point Lock is defined.
|
||||
class Lock {}
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate([Door, Lock]);
|
||||
const door = injector.get(Door);
|
||||
expect(door instanceof Door).toBeTruthy();
|
||||
expect(door.lock instanceof Lock).toBeTruthy();
|
||||
// #enddocregion
|
||||
});
|
||||
|
||||
it('can be unwrapped', () => {
|
||||
// #docregion resolve_forward_ref
|
||||
const ref = forwardRef(() => 'refValue');
|
||||
expect(resolveForwardRef(ref)).toEqual('refValue');
|
||||
expect(resolveForwardRef('regularValue')).toEqual('regularValue');
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
}
|
41
packages/examples/core/di/ts/injector_spec.ts
Normal file
41
packages/examples/core/di/ts/injector_spec.ts
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {InjectionToken, Injector, ReflectiveInjector} from '@angular/core';
|
||||
|
||||
export function main() {
|
||||
describe('injector metadata examples', () => {
|
||||
it('works', () => {
|
||||
// #docregion Injector
|
||||
const injector: Injector =
|
||||
ReflectiveInjector.resolveAndCreate([{provide: 'validToken', useValue: 'Value'}]);
|
||||
expect(injector.get('validToken')).toEqual('Value');
|
||||
expect(() => injector.get('invalidToken')).toThrowError();
|
||||
expect(injector.get('invalidToken', 'notFound')).toEqual('notFound');
|
||||
// #enddocregion
|
||||
});
|
||||
|
||||
it('injects injector', () => {
|
||||
// #docregion injectInjector
|
||||
const injector = ReflectiveInjector.resolveAndCreate([]);
|
||||
expect(injector.get(Injector)).toBe(injector);
|
||||
// #enddocregion
|
||||
});
|
||||
|
||||
it('should infer type', () => {
|
||||
// #docregion InjectionToken
|
||||
const BASE_URL = new InjectionToken<string>('BaseUrl');
|
||||
const injector =
|
||||
ReflectiveInjector.resolveAndCreate([{provide: BASE_URL, useValue: 'http://localhost'}]);
|
||||
const url = injector.get(BASE_URL);
|
||||
// here `url` is inferred to be `string` because `BASE_URL` is `InjectionToken<string>`.
|
||||
expect(url).toBe('http://localhost');
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
}
|
185
packages/examples/core/di/ts/metadata_spec.ts
Normal file
185
packages/examples/core/di/ts/metadata_spec.ts
Normal file
@ -0,0 +1,185 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Component, Directive, Host, Inject, Injectable, Optional, ReflectiveInjector, Self, SkipSelf} from '@angular/core';
|
||||
import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||
|
||||
export function main() {
|
||||
describe('di metadata examples', () => {
|
||||
describe('Inject', () => {
|
||||
it('works', () => {
|
||||
// #docregion Inject
|
||||
class Engine {}
|
||||
|
||||
@Injectable()
|
||||
class Car {
|
||||
constructor(@Inject('MyEngine') public engine: Engine) {}
|
||||
}
|
||||
|
||||
const injector =
|
||||
ReflectiveInjector.resolveAndCreate([{provide: 'MyEngine', useClass: Engine}, Car]);
|
||||
|
||||
expect(injector.get(Car).engine instanceof Engine).toBe(true);
|
||||
// #enddocregion
|
||||
});
|
||||
|
||||
it('works without decorator', () => {
|
||||
// #docregion InjectWithoutDecorator
|
||||
class Engine {}
|
||||
|
||||
@Injectable()
|
||||
class Car {
|
||||
constructor(public engine: Engine) {
|
||||
} // same as constructor(@Inject(Engine) engine:Engine)
|
||||
}
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate([Engine, Car]);
|
||||
expect(injector.get(Car).engine instanceof Engine).toBe(true);
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('Optional', () => {
|
||||
it('works', () => {
|
||||
// #docregion Optional
|
||||
class Engine {}
|
||||
|
||||
@Injectable()
|
||||
class Car {
|
||||
constructor(@Optional() public engine: Engine) {}
|
||||
}
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate([Car]);
|
||||
expect(injector.get(Car).engine).toBeNull();
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('Injectable', () => {
|
||||
it('works', () => {
|
||||
// #docregion Injectable
|
||||
@Injectable()
|
||||
class UsefulService {
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
class NeedsService {
|
||||
constructor(public service: UsefulService) {}
|
||||
}
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService]);
|
||||
expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);
|
||||
// #enddocregion
|
||||
});
|
||||
|
||||
it('throws without Injectable', () => {
|
||||
// #docregion InjectableThrows
|
||||
class UsefulService {}
|
||||
|
||||
class NeedsService {
|
||||
constructor(public service: UsefulService) {}
|
||||
}
|
||||
|
||||
expect(() => ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService])).toThrow();
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('Self', () => {
|
||||
it('works', () => {
|
||||
// #docregion Self
|
||||
class Dependency {}
|
||||
|
||||
@Injectable()
|
||||
class NeedsDependency {
|
||||
constructor(@Self() public dependency: Dependency) {}
|
||||
}
|
||||
|
||||
let inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]);
|
||||
const nd = inj.get(NeedsDependency);
|
||||
|
||||
expect(nd.dependency instanceof Dependency).toBe(true);
|
||||
|
||||
inj = ReflectiveInjector.resolveAndCreate([Dependency]);
|
||||
const child = inj.resolveAndCreateChild([NeedsDependency]);
|
||||
expect(() => child.get(NeedsDependency)).toThrowError();
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('SkipSelf', () => {
|
||||
it('works', () => {
|
||||
// #docregion SkipSelf
|
||||
class Dependency {}
|
||||
|
||||
@Injectable()
|
||||
class NeedsDependency {
|
||||
constructor(@SkipSelf() public dependency: Dependency) { this.dependency = dependency; }
|
||||
}
|
||||
|
||||
const parent = ReflectiveInjector.resolveAndCreate([Dependency]);
|
||||
const child = parent.resolveAndCreateChild([NeedsDependency]);
|
||||
expect(child.get(NeedsDependency).dependency instanceof Dependency).toBe(true);
|
||||
|
||||
const inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]);
|
||||
expect(() => inj.get(NeedsDependency)).toThrowError();
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('Host', () => {
|
||||
it('works', () => {
|
||||
// #docregion Host
|
||||
class OtherService {}
|
||||
class HostService {}
|
||||
|
||||
@Directive({selector: 'child-directive'})
|
||||
class ChildDirective {
|
||||
logs: string[] = [];
|
||||
|
||||
constructor(@Optional() @Host() os: OtherService, @Optional() @Host() hs: HostService) {
|
||||
// os is null: true
|
||||
this.logs.push(`os is null: ${os === null}`);
|
||||
// hs is an instance of HostService: true
|
||||
this.logs.push(`hs is an instance of HostService: ${hs instanceof HostService}`);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
viewProviders: [HostService],
|
||||
template: '<child-directive></child-directive>',
|
||||
})
|
||||
class ParentCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app',
|
||||
viewProviders: [OtherService],
|
||||
template: '<parent-cmp></parent-cmp>',
|
||||
})
|
||||
class App {
|
||||
}
|
||||
// #enddocregion
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [App, ParentCmp, ChildDirective],
|
||||
});
|
||||
|
||||
let cmp: ComponentFixture<App>;
|
||||
expect(() => cmp = TestBed.createComponent(App)).not.toThrow();
|
||||
|
||||
expect(cmp.debugElement.children[0].children[0].injector.get(ChildDirective).logs).toEqual([
|
||||
'os is null: true',
|
||||
'hs is an instance of HostService: true',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
149
packages/examples/core/di/ts/provider_spec.ts
Normal file
149
packages/examples/core/di/ts/provider_spec.ts
Normal file
@ -0,0 +1,149 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Inject, Injectable, InjectionToken, Optional, ReflectiveInjector} from '@angular/core';
|
||||
|
||||
export function main() {
|
||||
describe('Provider examples', () => {
|
||||
describe('TypeProvider', () => {
|
||||
it('works', () => {
|
||||
// #docregion TypeProvider
|
||||
@Injectable()
|
||||
class Greeting {
|
||||
salutation = 'Hello';
|
||||
}
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate([
|
||||
Greeting, // Shorthand for { provide: Greeting, useClass: Greeting }
|
||||
]);
|
||||
|
||||
expect(injector.get(Greeting).salutation).toBe('Hello');
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('ValueProvider', () => {
|
||||
it('works', () => {
|
||||
// #docregion ValueProvider
|
||||
const injector =
|
||||
ReflectiveInjector.resolveAndCreate([{provide: String, useValue: 'Hello'}]);
|
||||
|
||||
expect(injector.get(String)).toEqual('Hello');
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('MultiProviderAspect', () => {
|
||||
it('works', () => {
|
||||
// #docregion MultiProviderAspect
|
||||
const injector = ReflectiveInjector.resolveAndCreate([
|
||||
{provide: 'local', multi: true, useValue: 'en'},
|
||||
{provide: 'local', multi: true, useValue: 'sk'},
|
||||
]);
|
||||
|
||||
const locales: string[] = injector.get('local');
|
||||
expect(locales).toEqual(['en', 'sk']);
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('ClassProvider', () => {
|
||||
it('works', () => {
|
||||
// #docregion ClassProvider
|
||||
abstract class Shape { name: string; }
|
||||
|
||||
class Square extends Shape {
|
||||
name = 'square';
|
||||
}
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate([{provide: Shape, useClass: Square}]);
|
||||
|
||||
const shape: Shape = injector.get(Shape);
|
||||
expect(shape.name).toEqual('square');
|
||||
expect(shape instanceof Square).toBe(true);
|
||||
// #enddocregion
|
||||
});
|
||||
|
||||
it('is different then useExisting', () => {
|
||||
// #docregion ClassProviderDifference
|
||||
class Greeting {
|
||||
salutation = 'Hello';
|
||||
}
|
||||
|
||||
class FormalGreeting extends Greeting {
|
||||
salutation = 'Greetings';
|
||||
}
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate(
|
||||
[FormalGreeting, {provide: Greeting, useClass: FormalGreeting}]);
|
||||
|
||||
// The injector returns different instances.
|
||||
// See: {provide: ?, useExisting: ?} if you want the same instance.
|
||||
expect(injector.get(FormalGreeting)).not.toBe(injector.get(Greeting));
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('ExistingProvider', () => {
|
||||
it('works', () => {
|
||||
// #docregion ExistingProvider
|
||||
class Greeting {
|
||||
salutation = 'Hello';
|
||||
}
|
||||
|
||||
class FormalGreeting extends Greeting {
|
||||
salutation = 'Greetings';
|
||||
}
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate(
|
||||
[FormalGreeting, {provide: Greeting, useExisting: FormalGreeting}]);
|
||||
|
||||
expect(injector.get(Greeting).salutation).toEqual('Greetings');
|
||||
expect(injector.get(FormalGreeting).salutation).toEqual('Greetings');
|
||||
expect(injector.get(FormalGreeting)).toBe(injector.get(Greeting));
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
describe('FactoryProvider', () => {
|
||||
it('works', () => {
|
||||
// #docregion FactoryProvider
|
||||
const Location = new InjectionToken('location');
|
||||
const Hash = new InjectionToken('hash');
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate([
|
||||
{provide: Location, useValue: 'http://angular.io/#someLocation'}, {
|
||||
provide: Hash,
|
||||
useFactory: (location: string) => location.split('#')[1],
|
||||
deps: [Location]
|
||||
}
|
||||
]);
|
||||
|
||||
expect(injector.get(Hash)).toEqual('someLocation');
|
||||
// #enddocregion
|
||||
});
|
||||
|
||||
it('supports optional dependencies', () => {
|
||||
// #docregion FactoryProviderOptionalDeps
|
||||
const Location = new InjectionToken('location');
|
||||
const Hash = new InjectionToken('hash');
|
||||
|
||||
const injector = ReflectiveInjector.resolveAndCreate([{
|
||||
provide: Hash,
|
||||
useFactory: (location: string) => `Hash for: ${location}`,
|
||||
// use a nested array to define metadata for dependencies.
|
||||
deps: [[new Optional(), Location]]
|
||||
}]);
|
||||
|
||||
expect(injector.get(Hash)).toEqual('Hash for: null');
|
||||
// #enddocregion
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ElementFinder, browser, by, element} from 'protractor';
|
||||
import {verifyNoBrowserErrors} from '../../../../../_common/e2e_util';
|
||||
|
||||
describe('viewChild example', () => {
|
||||
afterEach(verifyNoBrowserErrors);
|
||||
let button: ElementFinder;
|
||||
let result: ElementFinder;
|
||||
|
||||
beforeEach(() => {
|
||||
browser.get('/core/di/ts/viewChild/index.html');
|
||||
button = element(by.css('button'));
|
||||
result = element(by.css('div'));
|
||||
});
|
||||
|
||||
it('should query view child', () => {
|
||||
expect(result.getText()).toEqual('Selected: 1');
|
||||
|
||||
button.click();
|
||||
|
||||
expect(result.getText()).toEqual('Selected: 2');
|
||||
});
|
||||
});
|
17
packages/examples/core/di/ts/viewChild/module.ts
Normal file
17
packages/examples/core/di/ts/viewChild/module.ts
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
|
||||
import {Pane, ViewChildComp} from './view_child_example';
|
||||
|
||||
@NgModule(
|
||||
{imports: [BrowserModule], declarations: [ViewChildComp, Pane], bootstrap: [ViewChildComp]})
|
||||
export class AppModule {
|
||||
}
|
37
packages/examples/core/di/ts/viewChild/view_child_example.ts
Normal file
37
packages/examples/core/di/ts/viewChild/view_child_example.ts
Normal file
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// #docregion Component
|
||||
import {Component, Directive, Input, ViewChild} from '@angular/core';
|
||||
|
||||
@Directive({selector: 'pane'})
|
||||
export class Pane {
|
||||
@Input() id: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'example-app',
|
||||
template: `
|
||||
<pane id="1" *ngIf="shouldShow"></pane>
|
||||
<pane id="2" *ngIf="!shouldShow"></pane>
|
||||
|
||||
<button (click)="toggle()">Toggle</button>
|
||||
|
||||
<div>Selected: {{selectedPane}}</div>
|
||||
`,
|
||||
})
|
||||
export class ViewChildComp {
|
||||
@ViewChild(Pane)
|
||||
set pane(v: Pane) {
|
||||
setTimeout(() => { this.selectedPane = v.id; }, 0);
|
||||
}
|
||||
selectedPane: string = '';
|
||||
shouldShow = true;
|
||||
toggle() { this.shouldShow = !this.shouldShow; }
|
||||
}
|
||||
// #enddocregion
|
24
packages/examples/core/di/ts/viewChild/view_child_howto.ts
Normal file
24
packages/examples/core/di/ts/viewChild/view_child_howto.ts
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// #docregion HowTo
|
||||
import {AfterViewInit, Component, Directive, ViewChild} from '@angular/core';
|
||||
|
||||
@Directive({selector: 'child-directive'})
|
||||
class ChildDirective {
|
||||
}
|
||||
|
||||
@Component({selector: 'someCmp', templateUrl: 'someCmp.html'})
|
||||
class SomeCmp implements AfterViewInit {
|
||||
@ViewChild(ChildDirective) child: ChildDirective;
|
||||
|
||||
ngAfterViewInit() {
|
||||
// child is set
|
||||
}
|
||||
}
|
||||
// #enddocregion
|
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
|
||||
import {ElementFinder, browser, by, element} from 'protractor';
|
||||
import {verifyNoBrowserErrors} from '../../../../../_common/e2e_util';
|
||||
|
||||
describe('viewChildren example', () => {
|
||||
afterEach(verifyNoBrowserErrors);
|
||||
let button: ElementFinder;
|
||||
let result: ElementFinder;
|
||||
|
||||
beforeEach(() => {
|
||||
browser.get('/core/di/ts/viewChildren/index.html');
|
||||
button = element(by.css('button'));
|
||||
result = element(by.css('div'));
|
||||
});
|
||||
|
||||
it('should query view children', () => {
|
||||
expect(result.getText()).toEqual('panes: 1, 2');
|
||||
|
||||
button.click();
|
||||
|
||||
expect(result.getText()).toEqual('panes: 1, 2, 3');
|
||||
});
|
||||
});
|
20
packages/examples/core/di/ts/viewChildren/module.ts
Normal file
20
packages/examples/core/di/ts/viewChildren/module.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
|
||||
import {Pane, ViewChildrenComp} from './view_children_example';
|
||||
|
||||
@NgModule({
|
||||
imports: [BrowserModule],
|
||||
declarations: [ViewChildrenComp, Pane],
|
||||
bootstrap: [ViewChildrenComp]
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// #docregion Component
|
||||
import {AfterViewInit, Component, Directive, Input, QueryList, ViewChildren} from '@angular/core';
|
||||
|
||||
@Directive({selector: 'pane'})
|
||||
export class Pane {
|
||||
@Input() id: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'example-app',
|
||||
template: `
|
||||
<pane id="1"></pane>
|
||||
<pane id="2"></pane>
|
||||
<pane id="3" *ngIf="shouldShow"></pane>
|
||||
|
||||
<button (click)="show()">Show 3</button>
|
||||
|
||||
<div>panes: {{serializedPanes}}</div>
|
||||
`,
|
||||
})
|
||||
export class ViewChildrenComp implements AfterViewInit {
|
||||
@ViewChildren(Pane) panes: QueryList<Pane>;
|
||||
serializedPanes: string = '';
|
||||
|
||||
shouldShow = false;
|
||||
|
||||
show() { this.shouldShow = true; }
|
||||
|
||||
ngAfterViewInit() {
|
||||
this.calculateSerializedPanes();
|
||||
this.panes.changes.subscribe((r) => { this.calculateSerializedPanes(); });
|
||||
}
|
||||
|
||||
calculateSerializedPanes() {
|
||||
setTimeout(() => { this.serializedPanes = this.panes.map(p => p.id).join(', '); }, 0);
|
||||
}
|
||||
}
|
||||
// #enddocregion
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// #docregion HowTo
|
||||
import {AfterViewInit, Component, Directive, QueryList, ViewChildren} from '@angular/core';
|
||||
|
||||
@Directive({selector: 'child-directive'})
|
||||
class ChildDirective {
|
||||
}
|
||||
|
||||
@Component({selector: 'someCmp', templateUrl: 'someCmp.html'})
|
||||
class SomeCmp implements AfterViewInit {
|
||||
@ViewChildren(ChildDirective) viewChildren: QueryList<ChildDirective>;
|
||||
|
||||
ngAfterViewInit() {
|
||||
// viewChildren is set
|
||||
}
|
||||
}
|
||||
// #enddocregion
|
Reference in New Issue
Block a user