- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当用户单击如下所示的按钮时,我正在尝试将动态组件加载到我的 View 中:
<button (click)="showContent()">Show Panel</button>
<!--This starts as 'showPanel = false'-->
<div *ngIf="showPanel">
<ng-template #ref></ng-template>
</div>
但是,当单击我 View 中的按钮时,我尝试运行 loadComponent()
但我收到一条错误消息 Cannot read property 'createComponent' of undefined
我已经查找了一些解决方案,有人建议使用 QueryList<ViewContainerRef>
但我没有成功让它发挥作用。
Source: ViewContainerRef is undefined when called in ngAfterViewInit
另一种解决方案建议使用 ElementRef
并检查已更改的状态,但即使在尝试检查它时也始终未定义 ngAfterViewInit
.
Source: @ViewChild in *ngIf
我正在寻找适用于 Angular 8 的选项,但我不完全确定下一步该去哪里。
代码如下:
parent.component.ts
export class ParentComponent {
@ViewChild('ref', { static: false, read: ViewContainerRef }) ref: ViewContainerRef;
showPanel = false;
loadComponent(): void {
const factory = this.componentFactoryResolver.resolveComponentFactory(ChildComponent);
const component = this.ref.createComponent(factory);
component.changeDetectorRef.detectChanges();
}
showContent(): void {
this.showPanel = !this.showPanel;
this.loadContent(); // Error is thrown here, this doesn't make sense as *ngIf should now be true.
}
最佳答案
按照 yurzui 的想法,下面是我将如何应用它们:
Here是 StackBlitz 示例。
showContent () {
this.showPanel = !this.showPanel;
if (this.showPanel) {
this.cdr.detectChanges();
this.loadComponent();
}
}
private _ref: ViewContainerRef;
private get ref () {
return this._ref;
}
@ViewChild('ref', { static: false, read: ViewContainerRef })
private set ref (r) {
console.log('setting ref', r)
this._ref = r;
if (this._ref) {
this.loadComponent();
}
}
showPanel = false;
constructor (
private cdr: ChangeDetectorRef,
private cfr: ComponentFactoryResolver,
) { }
loadComponent () {
const factory = this.cfr.resolveComponentFactory(ChildComponent);
const component = this.ref.createComponent(factory);
}
showContent () {
this.showPanel = !this.showPanel;
}
<ng-container>
正如您所指出的,使用 ngTemplateOutlet
通常是一个很好的解决方案,但是当您处理多个组件时,在模板中执行所有这些逻辑会变得很麻烦。
我们可以利用 ViewContainerRef
's用于处理组件中所有内容的 API ( .ts file
)。
<button (click)="showContent()">Show Panel</button>
<ng-container #vcr></ng-container>
@ViewChild('vcr', { static: true, read: ViewContainerRef })
vcr: ViewContainerRef;
showContent () {
this.showPanel = !this.showPanel;
this.showPanel && this.attachComponent();
!this.showPanel && this.removeComponent();
}
private attachComponent () {
const compFactory = this.cfr.resolveComponentFactory(ChildComponent);
const compView = this.vcr.createComponent(compFactory);
}
private removeComponent () {
this.vcr.clear();
}
这种方法给了你更多的控制权!
例如,您可以在 showPanel
之后保留组件的状态。使用 vcr.detach
变为假和 vcr.insert
.
关于angular - 在 *ngIf 中加载动态组件 - createComponent 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58646928/
问题 - 我将 ViewContainerRef 传递到我的构造函数中以实例化名为“MemberCardComponent”的组件。然后我尝试用输入数据填充它,但没有数据被填充到 innerHTML
我正在通过 createComponent 创建一个动态组件方法,但我在获取 child 时遇到问题组件更新它的 input从 parent 传入的值来自 component.instance.pro
我正在尝试通过单击按钮动态加载微调器组件。 Plunker 链接在这里, http://plnkr.co/edit/UEEQQRzX7RIkSO49rCHu?p=preview let element
我使用 viewContainerRef.createComponent 将动态组件加载到根组件中(......),但实际上它附加了错误的位置, 我的代码: -----app.compoment.ts
这更像是一个概念性问题。 我必须处理必须创建动态 h:dataTable 的功能.每当我创建一个组件时,我都会做类似的事情: DataTable table = (DataTable) FacesCo
我有自己的分析器和方法 protected TokenStreamComponents createComponents(final String fieldName, final Reader re
我在运行代码来创建简单的调查时遇到问题。我刚刚开始使用 UI,BoxLayout 给了我一个错误:线程“AWT-EventQueue-0”中的异常 java.awt.AWTError:BoxLayou
我在自定义分析器实现的 createComponents 实现中使用了 HTMLStripCharFilter,但 HTML 并未从内容中剥离。请在下面找到代码。 @Override prot
我有一个案例,我在列表中动态创建组件(经常刷新)并像这样使用 ngSwitch: 我想知道是否有更好更有效的方法,或者这是正
我正在尝试使用@ViewChild 将组件动态注入(inject)到父位置,但出现错误: Cannot read property 'createComponent' of undefined 在我尝
我对 createEmbeddedView 和 createComponent 的用例感到困惑,即何时使用哪个。 请提出一些可以说明在“动态创建场景”中使用其中任何一个的合适设置的案例。 最佳答案 请
当用户单击如下所示的按钮时,我正在尝试将动态组件加载到我的 View 中: Show Panel 但是,当单击我 View 中的按钮时,我尝试运行 loadComponent()但我收
我在我的 Angular 应用程序上实现了一个登录系统。我发现了一个好资源here为了那个原因。但是自定义 RouterOutlet 指令中有一个错误,如下所示: import { ElementRe
我是 Angular 4 的新手,我正在使用“动态组件加载”来制作项目,但是存在用于加载组件的 createComponent 问题。 上图中有四个部分。 顶部部分(headerComponent)
我做了一个弹出窗口。 Window window = (Window)Executions.createComponents("./org.zul", null, **map**); ./org.zu
我正在尝试在 angular4 中动态添加组件。我检查了其他问题,但我找不到解决方案。我得到了错误 ERROR TypeError: Cannot read property 'createCompo
当使用 karma 和 browserify 运行简单测试时,我的测试在 TestBed.createComponent() 函数内崩溃。 这是崩溃的 beforeEach: import { Com
我是一名优秀的程序员,十分优秀!