実行時に生成される動的生成コンポーネントがあります。以下は、.TSはダイナミックに生成されたcompoentを含むdiv上のngIf
`@ViewChild(TermsheetDirective) termHost: TermsheetDirective;
@Input() term;
@Input() title = '';
@Input() Qnumber = '';
@Output() result = new EventEmitter<any>();
section_title = '';
number = '';
component = [];
show = false;
constructor(private componentFactoryResolver: ComponentFactoryResolver) {
}
ngOnInit() {
this.number = this.Qnumber;
this.section_title = this.title;
}
ngAfterViewInit() {
}
ngAfterContentInit() {
}
loadcomponents() {
console.log(this.termHost);
for (let j = 0; j < this.term.components.length; j++) {
let termItem = this.term.components[j];
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(termItem.component);
let viewContainerRef = this.termHost.viewContainerRef;
let componentRef = viewContainerRef.createComponent(componentFactory);
(<TermComponent>componentRef.instance).data = termItem.data;
this.component[j] = componentRef;
}
}
getdata() {
let output = [];
for (let i = 0; i < this.component.length; i++) {
let temp = {
slug : this.section_title,
type : this.component[i].type,
value : this.component[i]._component.getdata()
};
output[i] = temp;
}
this.result.emit(output);
return output;
}
showcomp() {
console.log("In if");
this.show = true;
this.loadcomponents();
}
hidecomp() {
this.show = false;
}`
を提出し、次は私のhtml
`<div class="main">
<div class="div_for_ques">
<div class="question">
<div class="number">
{{number}}
</div>
<div class="textbox">
{{section_title}}
</div>
<div class="arrow" *ngIf="!show">
<a (click)="showcomp()" class="glyphicon"></a>
</div>
<div class="arrow" *ngIf="show">
<a (click)="hidecomp()" class="glyphicon"></a>
</div>
</div>
<div class="sec_two" *ngIf="show">
<ng-template term-host></ng-template>
</div>
</div>
</div>`
である私は、特定のボタンがクリックされたときにのみ表示されるように動的に生成されたコンポーネントが含まれているのdivをしたいです。私は次のような対応をしています。
しかし、このdivをngなしで表示しようとすると、正常に動作しています。しかし、ngIfのtermHostは未定義です!誰かがここで起こっていることを説明してもらえますか?
私はchangeDetectorを試みますが、運はありません!私はthis.changeDetector.detectChanges()の後にロードコンポーネントを呼び出しましたが、同じエラーが発生しました! –
あなたはクエリーリストを使用する方法の小さな例を提供できますか? –