0

我们可以使用它的选择器在 Angular 中动态加载一个组件吗?我需要从 HTML 访问组件模块

@Component({
    moduleId: module.id.toString(),
    selector: 'dashboard',
    templateUrl: 'dashboard.component.html'
})
export class DashboardComponent implements OnInit {
}


@Component({
    moduleId: module.id.toString(),
    selector: 'my-component',
    templateUrl: 'my-component.html',
    styleUrls: ['my-component.css']   
 })
 export class MyComponent{

loadComponent = (selector: string, module: any): void => {
        this._compiler.compileModuleAndAllComponentsAsync(module).then(_module => {
            let _componentFactories = _module.componentFactories.filter(_c => {
                // here I am using the selector
                return _c.selector === selector.toLocaleLowerCase();
            });

        });
    }
 }

HTML

{{loadComponent('dashboard',moduleId)}}

在 Angular 2 中使用 selector 动态跟踪 Load 组件,这会引发以下错误:

No NgModule metadata found for 'undefined'.
4

0 回答 0