简体   繁体   English

将数据传递给动态创建的 angular 模块和组件

[英]Pass data to dynamically created angular modules and components

I need to pass data to a dynamically created angular module like an id or JSON my controller code is like this.我需要将数据传递给动态创建的角度模块,例如 id 或 JSON,我的控制器代码是这样的。

Logic Explained - The API provides the layout of the page if primary module comes first or secondary module (There are more which cannot be handled by an ngif) all modules have different designs primary and secondary have n no of styles which are also decided by the API too.逻辑解释 - API 提供页面布局,如果主模块先出现或次要模块(还有更多不能由 ngif 处理)所有模块都有不同的设计,主要和次要有 n 种样式,这些样式也由API也是。 The id is the key to decide all the designs of that page id 是决定该页面所有设计的关键

export class MainLayoutComponent implements OnInit {

 modules: any = [
  {
    module: PrimaryModule,
    component: PrimaryLandingPageComponent,
    id: 1
  },
  {
    module: SecondaryModule,
    component: SecondaryLandingPageComponent,
    id: 2
  },
 ];

 constructor(public compiler: Compiler) {  }

 returnCompiledModule(moduleItem) {
   return this.compiler.compileModuleSync(moduleItem);
 }

 ngOnInit() {
   console.log('hello');
 }

}

why I am doing like this is because I have the layout of the page which is decided by the API and each module has different types of components with different designs (That code has been omitted) This is how I am rendering in HTML为什么我这样做是因为我有由 API 决定的页面布局,每个模块都有不同类型的组件,具有不同的设计(该代码已被省略)这就是我在 HTML 中呈现的方式

<ng-container *ngFor="let module of modules">
  <ng-container 
    *ngComponentOutlet="module.component; 
    ngModuleFactory: returnCompiledModule(module.module);">
  </ng-container>
</ng-container>

Is there any way I can pass data to the module or the component corresponding to it the id values in the JSON or the JSON itself via the router, another syntactical sweetener I may be missing, services or the compiler itself I have been stuck here for some time any help would be appreciated or is there any other way to do this is in angular version 8 without IVY Thanks in advance.有什么方法可以通过路由器将数据传递给模块或与其对应的组件 JSON 中的 id 值或 JSON 本身,我可能缺少的另一种语法甜味剂,服务或编译器本身我一直被困在这里一段时间后,任何帮助将不胜感激,或者有没有其他方法可以在没有 IVY 的 Angular 版本 8 中执行此操作,提前致谢。

In order to pass data to dynamically created components using ngComponentOutlet , you can use Injector .为了使用ngComponentOutlet将数据传递给动态创建的组件,您可以使用Injector

Just add this function to your MainLayoutComponent :只需将此功能添加到您的MainLayoutComponent

getInjector(moduleItem) {
  return Injector.create([
    { provide: Number, useValue: moduleItem.id }
  ], this.injector);
}

and change its constructor to inject Injector object:并将其构造函数更改为注入Injector对象:

constructor(public compiler: Compiler, private injector: Injector) {  }

Then - as you can see in the docs - inside ngComponentOutlet you can pass the data by specifying the injector like this:然后 - 正如您在文档中看到的 - 在ngComponentOutlet您可以通过指定注入器来传递数据,如下所示:

<ng-container *ngFor="let module of modules">
  <ng-container 
    *ngComponentOutlet="module.component; 
                        ngModuleFactory: returnCompiledModule(module.module);
                        injector: getInjector(module);">
  </ng-container>
</ng-container>

Now each of your Component classes which can be dynamically created ( PrimaryLandingPageComponent , SecondaryLandingPageComponent etc.) should contain id argument in its constructor which you can further use as needed:现在,您可以动态创建的每个Component类( PrimaryLandingPageComponentSecondaryLandingPageComponent等)都应在其构造函数中包含id参数,您可以根据需要进一步使用:

constructor(id: Number) { 
  // do whatever you need with id value
}

I haven't tested this code so you would probably need to fix some issues, but hopefully you get the idea.我还没有测试过这段代码,所以你可能需要解决一些问题,但希望你能明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM