绑定在动态加载的组件中不起作用
Bindings not working in dynamically loaded component
我遇到一个问题,如果我动态加载一个组件,模板中的绑定都不适合我。此外,ngOnInit
方法永远不会被触发。
loadView() {
this._dcl.loadAsRoot(Injected, null, this._injector).then(component => {
console.info('Component loaded');
})
}
动态加载组件
import {Component, ElementRef, OnInit} from 'angular2/core'
declare var $:any
@Component({
selector: 'tester',
template: `
<h1>Dynamically loaded component</h1>
<span>{{title}}</span>
`
})
export class Injected implements OnInit {
public title:string = "Some text"
constructor(){}
ngOnInit() {
console.info('Injected onInit');
}
}
这是我第一次使用动态加载的组件,所以我认为可能是试图错误地实现它。
这是一个演示这个问题的庞克。如有任何帮助,我们将不胜感激。
loadAsRoot
的使用有关的已知错误。建议的解决方法是使用loadNextToLocation
或loadIntoLocation
。
对我来说,这是有问题的,因为我试图动态加载的组件是一个位于fixed
css位置的组件内部的模式对话框。我还希望能够从任何组件加载模态,并将其注入DOM中的同一位置,而不管它是从哪个组件动态加载的。
我的解决方案是使用forwardRef
将我的根AppComponent
注入到想要动态加载我的模态的组件中。
constructor (
.........
.........
private _dcl: DynamicComponentLoader,
private _injector: Injector,
@Inject(forwardRef(() => AppComponent)) appComponent) {
this.appComponent = appComponent;
}
在我的AppComponent
中,我有一个返回应用程序的ElementRef
的方法
@Component({
selector: 'app',
template: `
<router-outlet></router-outlet>
<div #modalContainer></div>
`,
directives: [RouterOutlet]
})
export class AppComponent {
constructor(public el:ElementRef) {}
getElementRef():ElementRef {
return this.el;
}
}
回到我的另一个组件(我想从中动态加载模态的组件),我现在可以调用:
this._dcl.loadIntoLocation(ModalComponent, this.appComponent.getElementRef(), 'modalContainer').then(component => {
console.log('Component loaded')
})
也许这将帮助其他有类似问题的人。
无需从DOM中清除组件实例。使用angular2/core包中的"componentRef"来创建和处理组件实例。使用show()在所需位置加载模态组件,使用hide()在第二次调用loadIntoLocation之前处理组件实例。
例如:
@Component({
selector: 'app',
template: `
<router-outlet></router-outlet>
<div #modalContainer></div>
`,
directives: [RouterOutlet]
})
export class AppComponent {
private component:Promise<ComponentRef>;
constructor(public el:ElementRef) {}
getElementRef():ElementRef {
return this.el;
}
show(){
this.component = this._dcl.loadIntoLocation(ModalComponent,this.appComponent.getElementRef(), 'modalContainer').then(component => {console.log('Component loaded')})
}
hide(){
this.component.then((componentRef:ComponentRef) => {
componentRef.dispose();
return componentRef;
});
}
}
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- React AppendChild组件没有'不起作用
- 带有组件标签的 VueJS 路由不起作用
- Angular 1.5组件双向绑定不起作用
- 在引导选择组件中的搜索不起作用,其中数据令牌包含所有数字;4730”;
- 主干调用在 React 组件函数中不起作用
- Firefox 附加组件 SDK Worker.port 事件不起作用
- 余烬JS组件观察器不起作用
- 初始使用引导折叠组件不起作用
- Angular 1.5 组件 onChanges 不起作用
- 为什么我的应用程序中的某些组件在IE7中工作,而在IE9中不起作用
- 拖动的组件不起作用
- 绑定在动态加载的组件中不起作用
- 带有淘汰SPA的HTML尝试使用组件,绑定不起作用
- Javascript组件在IE9中不起作用
- 调用Coldfusion组件函数的jQuery-post方法不起作用
- Meteor React组件onClick事件在IE中不起作用
- 向容器中添加组件不起作用
- 浏览器的 reactjs 捆绑脚本在客户端不起作用(未捕获的引用错误:未定义组件)
- 在角度预类型组件中解析数据不起作用