import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class DataService {
private data: any;
private subject: Subject<any> = new Subject<any>();
setData(data: any): void {
this.data = data;
this.subject.next(data);
}
getData(): Observable<any> {
return this.subject.asObservable();
}
}
import { Component, OnInit } from '@angular/core';
import { DataService } from './shared/Dataservice';
@Component({
moduleId: module.id,
selector: '<my-component></my-component>',
templateUrl: '.name.component.html',
providers: [DataService]
})
export class MyComponent implements OnInit {
constructor(private dataService: DataService) {}
ngOnInit() {
// Will invoke data handler everytime
// when other component use the setter this.dataService.setData()
this.dataService.getData().subscribe(data => console.log(data));
}
}
import { Component, Input, OnInit } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'child-component',
template: `I'm {{ name }}`
})
export class ChildComponent implements OnInit {
@Input() name: string;
constructor() { }
ngOnInit() { }
}
import { Component, OnInit } from '@angular/core';
import { ChildComponent } from './child-component';
@Component({
moduleId: module.id,
selector: 'parent-component',
template: `<child-component [name]="childName"></child-component>`,
// This is unnecessary when installing ChildComponent within Root NgModule
directives: [ChildComponent]
})
export class ParentComponent implements OnInit {
private childName: string;
constructor() { }
ngOnInit() {
this.childName = 'StevenShen';
}
}
<child-component [name]="childName"></child-component>
import { Component, Input, SimpleChanges } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'child-component',
template: `I'm {{ name }}`
})
export class ChildComponent {
@Input() name: string;
ngOnChanges(changes: SimpleChanges) {
this.name = changes['childName'].currentValue;
}
}
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'name-parent',
template: `
<h2>Master controls {{names.length}} names</h2>
<name-child *ngFor="let name of names" [name]="name"></name-child>
`
})
export class ParentComponent {
names = ['StevenShen', ' ', ' lei '];
}
import { Component, Input } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'name-child',
template: `<h3>"{{name}}"</h3>`
})
export class ChildComponent {
name: string = 'default name';
@Input()
set name(name: string) {
this.name = (name && name.trim()) || 'default name';
}
get name() { return this.name; }
}
@Component({
moduleId: module.id,
selector: 'child-component',
template: `I'm {{ name }}`
})
export class ChildComponent {
@Input() name: string;
@Output() say: EventEmitter<boolean> = new EventEmitter<boolean>();
ngOnChanges(changes: SimpleChange) {
this.name = changes['childName'].currentValue;
}
speak() {
this.say.emit(true);
}
}
import { Component, OnInit } from '@angular/core';
import { ChildComponent } from './child-component';
@Component({
moduleId: module.id,
selector: 'parent-component',
template: `<child-component [name]="childName" (say)="isChildSpeak($event)"></child-component>`,
// This is unnecessary when installing ChildComponent within Root NgModule
directives: [ChildComponent]
})
export class ParentComponent implements OnInit {
private childName: string;
constructor() { }
ngOnInit() {
this.childName = 'StevenShen';
}
isChildSpeak(isIt: boolean) {
console.log('The child speak status is %s', isIt ? 'ture' : 'false');
}
}
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'child-component',
template: `I'm {{ name }}`
})
export class ChildComponent {
public name: string;
speak() {
console.log('say something whitout EventEmitter');
}
}
import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
import { ChildComponent } from './child-component.ts';
@Component({
moduleId: module.id,
selector: 'parent-component',
// attention #childCmp tag
template: `
<child-component #childCmp></child-component>
<button (click)="child.name = childName"></button>
`,
// This is unnecessary when installing ChildComponent within Root NgModule
directives: [ ChildComponent ]
})
export class ParentComponent implements OnInit, AfterViewInit {
@ViewChild('childCmp') childCmp: ElementRef;
constructor() { }
ngOnInit() {
this.childCmp.name = 'StevenShen';
}
ngAfterViewInit() {
this.childCmp.speak();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有