docs(aio): rename cb- files and a few others
This commit is contained in:

committed by
Pete Bacon Darwin

parent
c3fa8803d3
commit
93516ea8a1
@ -0,0 +1,57 @@
|
||||
// #docregion
|
||||
import { Component, Input, AfterViewInit, ViewChild, ComponentFactoryResolver, OnDestroy } from '@angular/core';
|
||||
|
||||
import { AdDirective } from './ad.directive';
|
||||
import { AdItem } from './ad-item';
|
||||
import { AdComponent } from './ad.component';
|
||||
|
||||
@Component({
|
||||
selector: 'add-banner',
|
||||
// #docregion ad-host
|
||||
template: `
|
||||
<div class="ad-banner">
|
||||
<h3>Advertisements</h3>
|
||||
<ng-template ad-host></ng-template>
|
||||
</div>
|
||||
`
|
||||
// #enddocregion ad-host
|
||||
})
|
||||
// #docregion class
|
||||
export class AdBannerComponent implements AfterViewInit, OnDestroy {
|
||||
@Input() ads: AdItem[];
|
||||
currentAddIndex: number = -1;
|
||||
@ViewChild(AdDirective) adHost: AdDirective;
|
||||
subscription: any;
|
||||
interval: any;
|
||||
|
||||
constructor(private _componentFactoryResolver: ComponentFactoryResolver) { }
|
||||
|
||||
ngAfterViewInit() {
|
||||
this.loadComponent();
|
||||
this.getAds();
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
|
||||
loadComponent() {
|
||||
this.currentAddIndex = (this.currentAddIndex + 1) % this.ads.length;
|
||||
let adItem = this.ads[this.currentAddIndex];
|
||||
|
||||
let componentFactory = this._componentFactoryResolver.resolveComponentFactory(adItem.component);
|
||||
|
||||
let viewContainerRef = this.adHost.viewContainerRef;
|
||||
viewContainerRef.clear();
|
||||
|
||||
let componentRef = viewContainerRef.createComponent(componentFactory);
|
||||
(<AdComponent>componentRef.instance).data = adItem.data;
|
||||
}
|
||||
|
||||
getAds() {
|
||||
this.interval = setInterval(() => {
|
||||
this.loadComponent();
|
||||
}, 3000);
|
||||
}
|
||||
}
|
||||
// #enddocregion class
|
@ -0,0 +1,6 @@
|
||||
// #docregion
|
||||
import { Type } from '@angular/core';
|
||||
|
||||
export class AdItem {
|
||||
constructor(public component: Type<any>, public data: any) {}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
// #docregion
|
||||
export interface AdComponent {
|
||||
data: any;
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
// #docregion
|
||||
import { Directive, ViewContainerRef } from '@angular/core';
|
||||
|
||||
@Directive({
|
||||
selector: '[ad-host]',
|
||||
})
|
||||
export class AdDirective {
|
||||
constructor(public viewContainerRef: ViewContainerRef) { }
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
// #docregion
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
import { HeroJobAdComponent } from './hero-job-ad.component';
|
||||
import { HeroProfileComponent } from './hero-profile.component';
|
||||
import { AdItem } from './ad-item';
|
||||
|
||||
@Injectable()
|
||||
export class AdService {
|
||||
getAds() {
|
||||
return [
|
||||
new AdItem(HeroProfileComponent, {name: 'Bombasto', bio: 'Brave as they come'}),
|
||||
|
||||
new AdItem(HeroProfileComponent, {name: 'Dr IQ', bio: 'Smart as they come'}),
|
||||
|
||||
new AdItem(HeroJobAdComponent, {headline: 'Hiring for several positions',
|
||||
body: 'Submit your resume today!'}),
|
||||
|
||||
new AdItem(HeroJobAdComponent, {headline: 'Openings in all departments',
|
||||
body: 'Apply today'}),
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
// #docregion
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
|
||||
import { AdService } from './ad.service';
|
||||
import { AdItem } from './ad-item';
|
||||
|
||||
@Component({
|
||||
selector: 'my-app',
|
||||
template: `
|
||||
<div>
|
||||
<add-banner [ads]="ads"></add-banner>
|
||||
</div>
|
||||
`
|
||||
})
|
||||
export class AppComponent implements OnInit {
|
||||
ads: AdItem[];
|
||||
|
||||
constructor(private adService: AdService) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.ads = this.adService.getAds();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
// #docregion
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
import { NgModule } from '@angular/core';
|
||||
import { AppComponent } from './app.component';
|
||||
import { HeroJobAdComponent } from './hero-job-ad.component';
|
||||
import { AdBannerComponent } from './ad-banner.component';
|
||||
import { HeroProfileComponent } from './hero-profile.component';
|
||||
import { AdDirective } from './ad.directive';
|
||||
import { AdService } from './ad.service';
|
||||
|
||||
@NgModule({
|
||||
imports: [ BrowserModule ],
|
||||
providers: [AdService],
|
||||
declarations: [ AppComponent,
|
||||
AdBannerComponent,
|
||||
HeroJobAdComponent,
|
||||
HeroProfileComponent,
|
||||
AdDirective ],
|
||||
// #docregion entry-components
|
||||
entryComponents: [ HeroJobAdComponent, HeroProfileComponent ],
|
||||
// #enddocregion entry-components
|
||||
bootstrap: [ AppComponent ]
|
||||
})
|
||||
export class AppModule {
|
||||
constructor() {}
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
// #docregion
|
||||
import { Component, Input } from '@angular/core';
|
||||
|
||||
import { AdComponent } from './ad.component';
|
||||
|
||||
@Component({
|
||||
template: `
|
||||
<div class="job-ad">
|
||||
<h4>{{data.headline}}</h4>
|
||||
|
||||
{{data.body}}
|
||||
</div>
|
||||
`
|
||||
})
|
||||
export class HeroJobAdComponent implements AdComponent {
|
||||
@Input() data: any;
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
// #docregion
|
||||
import { Component, Input } from '@angular/core';
|
||||
|
||||
import { AdComponent } from './ad.component';
|
||||
|
||||
@Component({
|
||||
template: `
|
||||
<div class="hero-profile">
|
||||
<h3>Featured Hero Profile</h3>
|
||||
<h4>{{data.name}}</h4>
|
||||
|
||||
<p>{{data.bio}}</p>
|
||||
|
||||
<strong>Hire this hero today!</strong>
|
||||
</div>
|
||||
`
|
||||
})
|
||||
export class HeroProfileComponent implements AdComponent {
|
||||
@Input() data: any;
|
||||
}
|
||||
|
||||
|
26
aio/content/examples/dynamic-component-loader/src/index.html
Normal file
26
aio/content/examples/dynamic-component-loader/src/index.html
Normal file
@ -0,0 +1,26 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<base href="/">
|
||||
<title>Dynamic Component Loader</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
<link rel="stylesheet" href="sample.css">
|
||||
|
||||
<script src="node_modules/core-js/client/shim.min.js"></script>
|
||||
|
||||
<script src="node_modules/zone.js/dist/zone.js"></script>
|
||||
<script src="node_modules/reflect-metadata/Reflect.js"></script>
|
||||
<script src="node_modules/systemjs/dist/system.src.js"></script>
|
||||
|
||||
<script src="systemjs.config.js"></script>
|
||||
<script>
|
||||
System.import('main.js').catch(function(err){ console.error(err); });
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<my-app>Loading app...</my-app>
|
||||
</body>
|
||||
|
||||
</html>
|
@ -0,0 +1,6 @@
|
||||
// #docregion
|
||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||
import { AppModule } from './app/app.module';
|
||||
|
||||
platformBrowserDynamic().bootstrapModule(AppModule);
|
||||
|
23
aio/content/examples/dynamic-component-loader/src/sample.css
Normal file
23
aio/content/examples/dynamic-component-loader/src/sample.css
Normal file
@ -0,0 +1,23 @@
|
||||
.hero-profile {
|
||||
border: 1px solid gray;
|
||||
padding: 5px;
|
||||
padding-bottom: 20px;
|
||||
padding-left: 20px;
|
||||
border-radius: 10px;
|
||||
background-color: lightgreen;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.job-ad {
|
||||
border: 1px solid gray;
|
||||
padding: 5px;
|
||||
padding-bottom: 20px;
|
||||
padding-left: 20px;
|
||||
border-radius: 10px;
|
||||
background-color: lightblue;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.ad-banner {
|
||||
width: 400px;
|
||||
}
|
Reference in New Issue
Block a user