gpt4 book ai didi

angular - QueryList 未使用动态组件更新

转载 作者:太空狗 更新时间:2023-10-29 18:05:54 33 4
gpt4 key购买 nike

我有一个父组件,它可以有同一个子组件的多个实例。这里棘手的部分是子组件的实例在 init 时可用,并且基于子组件内的某些逻辑,我使用输出事件发射器从父组件重新生成另一个子组件。但是,保存时我只看到我放入 html 模板的子组件的第一个实例,而不是动态生成的子组件。我确实看到了其他类似的问题,但似乎没有一个有同样的问题。我在这里缺少什么?

如有任何帮助,我们将不胜感激。

更新 - 开始

添加了一个 plunker 来演示问题,如果有人想玩并找到原因

https://plnkr.co/edit/FW49ztzsg5uyXF7DtHDY?p=preview

更新 - 结束

父组件

import { Component, OnInit, ViewContainerRef, ViewChild, ComponentFactoryResolver, ViewChildren, QueryList } from '@angular/core';
import { ChildComponent } from './child/child.component';

@Component( {
selector: 'parent-cmp',
entryComponents: [ChildComponent],
template: `
<child-cmp (onSomeSelectionChange)="onSomeSelectionChange($event)"></child-cmp>
<div #childCompSection></div>
<button (click)="save()">Save</button>
`
} )
export class ParentComponent implements OnInit {

@ViewChild( 'childCompSection', { read: ViewContainerRef } ) childCompSection: any;
currentComponentHolder: any;
@ViewChildren( ChildComponent ) childComponents: QueryList<ChildComponent>;

constructor( private resolver: ComponentFactoryResolver ) { }

ngOnInit() {
// do some stuff to load initial view, nothing done using childComponents
}

onSomeSelectionChange( someValue ) {
if ( someValue !== 3 ) {
let componentFactory = this.resolver.resolveComponentFactory( ChildComponent );
this.currentComponentHolder = this.childCompSection.createComponent( componentFactory );
this.currentComponentHolder.instance.onSomeSelectionChange.subscribe( data => this.onSomeSelectionChange( data ) );
}
}

// Need all child components (which includes dynamically generated components) here
save() {
console.log(this.childComponents); // This just prints the child-cmp object that is already added not the ones that are dynamically added via ComponentFactoryResolver
}

}

子组件

import { Component, Output, EventEmitter } from '@angular/core';
import { ChildComponent } from './child/child.component';

@Component( {
selector: 'child-cmp',
template: `
<div>
<select [(ngModel)]="selectedValue" (ngModelChange)="showNumberField($event)">
<option value="0">Select...</option>
<option value="1">Add...</option>
<option value="2">Subtract...</option>
<option selected value="3">End</option>
</select>
<div *ngIf="showField">
<label>Number:</label><input type="number" [(ngModel)]="numValue">
</div>
</div>
`
} )
export class ChildComponent {

@Output() onSomeSelectionChange = new EventEmitter<Lookup>();
selectedValue: number;
numValue: number;
showField: boolean = false;

showNumberField(value) {
if(value !== 3) {
showField = true;
}
this.onSomeSelectionChange.emit( value ); // This does work fine and another instance of the child component is generated from parent component but QueryList in parent component does not get updated
}
}

最佳答案

我遇到了同样的问题,发现您可以订阅查询列表更改,如果您添加或删除元素,这些更改将触发。希望能帮助其他人......

@ViewChildren(MyComponent) myComp: QueryList<MyComponent>;

ngAfterViewInit() {
this.myComp.changes
.subscribe((queryChanges) => {
// Do something
});
}

关于angular - QueryList 未使用动态组件更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43947963/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com