희망 사항입니다.
라이브 데모 : http://plnkr.co/edit/SPbo1Cw7mDadfLK3ElEC?p=preview
SRC/동적 component.ts
import 'rxjs/Rx';
export default class DynamicComponent {
myData:any;
@Input() set componentData(data: {component: any, inputs: any }) {
...
this.myData=data; //assinging parent data object to myData object.
...
...
component.instance.showNum=this.myData.inputs.showNum
//component.instance.someNumber syntax allows you to pass varible to dynamically created component
//here, I'm using subject from rxjs and subscribing to it as below
component.instance.emitNumber$.subscribe(v=>{
console.log('getting'+ v);
console.log(this.myData);
this.myData.inputs.showNum=v; //assigning subscribed value back to parent object
console.log(this.myData);
});
}
SRC/안녕하세요 - 세계 component.ts
import {Observable,Subject} 'rxjs/Rx';
@Component({
selector: 'hello-world',
template: `
...
<input [(ngModel)]="showNum" (keyup)="updateValue(showNum)" type="text">
`,
export default class HelloWorldComponent {
@Input() showNum:number;
emitNumber=new Subject<number>(); //using rxjs subject
emitNumber$=this.emitNumber.asObservable();
updateValue(val){
this.emitNumber.next(val); //emitting value back to dyanmic component
}
}
})
부모 세계 안녕하세요 - component.ts와 같은
<div *ngFor="let x of components">showNum of parent: {{x.inputs.showNum}}<br></div>
.
이 방법을 사용하면 동적으로 생성 된 구성 요소에 상속을 사용할 수 있습니까? 예를 들어, 동적으로 생성 된 구성 요소의 템플리트가 상속 할 수있는 일종의 기본 템플리트를 갖고 싶습니다. 이것이 가능한가? – 7ball