docs(core): add an example of using ContentChild
This commit is contained in:
parent
a2bf334e6e
commit
ff03d87cdd
|
@ -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,29 @@
|
||||||
|
/**
|
||||||
|
* @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 {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');
|
||||||
|
});
|
||||||
|
});
|
|
@ -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 {
|
||||||
|
}
|
Loading…
Reference in New Issue