- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我对 Angular 2 rc5 中的动态组件创建有疑问。
那么让我们假设我们有两个普通的 Angular 分量:
@Component({
template: `
<div id="container">
<h1>My Component</h1>
</div>
`,
selector: 'my-app'
})
export class AppComponent { }
@Component({
template: '<p>{{text}}</p>',
selector: 'simple-cmp'
})
export class SimpleComponent { public text='Hello World!' }
然后一些外部非 Angular 代码块修改 DOM:
let newNode = document.createElement('div');
newNode.id = 'placeholder';
document.getElementById('container').appendChild(newNode);
这是经过操作后的一些推测树:
<div id="container">
<h1>My Component</h1>
<div id="placeholder"></div>
</div>
所以我要做的就是将 SimpleComoponent 实例动态添加到#placeholder div 中。我怎样才能达到这个结果?
我一直在尝试使用 ComponentFactory.createComponent(injector, [], newNode),虽然它添加了组件,但生命周期 Hook 和绑定(bind)都不起作用。
我相信有一些方法可以使用 ViewContainerRef 来实现它,但我如何才能将它与动态创建的节点链接起来?
这是我期望的结果
<div id="container">
<h1>My Component</h1>
<div id="placeholder">
<simple-cmp>Hello world!</simple-cmp>
</div>
</div>
谢谢!
最佳答案
When creating a component您可以传递将充当所创建组件的宿主元素的 DOM 节点:
create(injector: Injector, projectableNodes?: any[][], rootSelectorOrNode?: string|any, ngModule?: NgModuleRef): ComponentRef
但由于此组件不是任何其他组件的子组件,您必须手动将其附加到 ApplicationRef 以便进行更改检测。
所以这是你需要做的:
1) 创建一个组件,指定要添加到的根节点。
2) 将 View 附加到 ApplicationRef 以便进行更改检测。您仍然没有 Input
和 ngOnChanges
操作,但 DOM 更新将正常工作。
@Component({
template: `
<div id="container">
<h1>My Component</h1>
</div>
`,
selector: 'my-app'
})
export class AppComponent {
constructor(private resolver: ComponentFactoryResolver,
private injector: Injector,
private app: ApplicationRef) {
}
addDynamicComponent() {
let factory = this.resolver.resolveComponentFactory(SimpleComponent);
let newNode = document.createElement('div');
newNode.id = 'placeholder';
document.getElementById('container').appendChild(newNode);
const ref = factory.create(this.injector, [], newNode);
this.app.attachView(ref.hostView);
}
}
关于Angular 2 在不使用 ViewContainerRef 的情况下将组件动态插入到特定的 DOM 节点中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39357195/
假设我们有以下内容: @Directive({ selector: "[appSome]" }) export class SomeDirective { public constructor
假设我们有以下内容: @Directive({ selector: "[appSome]" }) export class SomeDirective { public constructor
有一种情况,我想在单击按钮时清除“viewContainer”,但它显示错误 ERROR TypeError: Cannot read property 'viewContainer' of unde
只是尝试阐明 ViewContainerRef.createComponent 中索引参数的含义: createComponent( componentFactory: ComponentFa
我想在 Angular 4 中创建嵌套组件 这是选择器组件 import {InputComponent} from './input/input.component' import {BlockCo
我正在尝试显示一个类似于(不完全) Angular 文档中示例的动态组件。 我有一个带有 viewContainerRef 的动态指令 @Directive({ selector: '[dyna
我正在创建一个“组件工厂”来帮助我动态创建组件。该工厂用于创建类似的组件; PopOvers、Modals、Overlays 等 它有一个 attach 方法来创建组件: attach(compone
升级到 Angular 7 后,我得到 parentInjector is deprecated: No replacement在我的代码中。由于这显然已被弃用,为什么没有可用的更新,或者 Angul
给定用于 Dynamic Component Loading 的指令与 ViewContainerRef注入(inject): import { Directive, ViewContainerRef
所以我目前正在尝试创建嵌套评论,并且我目前正在引入一个动态表单组件,以便人们可以回复评论。当只有 parent 和 child 时,它效果很好,但如果有 sibling ,它会选择第一个。例如, --
所以我目前正在尝试创建嵌套评论,并且我目前正在引入一个动态表单组件,以便人们可以回复评论。当只有 parent 和 child 时,它效果很好,但如果有 sibling ,它会选择第一个。例如, --
什么更好?使用 ngFor 或 ViewContainerRef 动态创建组件?有什么区别? 例如,如果我有一个创建新元素的按钮,每次按下它都会生成一个新组件。 1) 第一个选项如下 items: n
我有一个组件: @Component({ selector: 'vcrdi', template: ` ViewContainerRef DI `, })
我使用 viewContainerRef.createComponent 将动态组件加载到根组件中(......),但实际上它附加了错误的位置, 我的代码: -----app.compoment.ts
当我使用 ViewContainerRef 创建组件并将实例分配给负责创建子组件的父组件的属性时,是否需要将此属性设置为 null 如果我想释放内存,在调用 ViewContainerRef.clea
有没有办法从动态创建的组件中获取 ViewContainerRef?我动态创建的组件内部有一个 ngContent 元素,我想在动态创建后填充它。 export class Example { @
我是这样动态添加组件的: export class CustomersOverviewComponent implements OnInit, OnDestroy { @ViewChild(Pan
嘿,我现在正在逐步学习 Angular 2,并且我已经构建了一个名为 except 的结构自定义指令。除非行为与 ngIf act 相同,否则如果我的条件为真,则将元素附加到 dom,否则将其分离。这
我正在尝试获取 ViewContainerRef 中动态创建的组件的 index 我需要获取索引,这样我也可以销毁组件。 代码如下 @ViewChild('dynamicInsert', { read
@Component({ selector: 'my-cmp', template: ` ` }) export class MyCmp { @ViewChild('target',
我是一名优秀的程序员,十分优秀!