Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. import.....
  2.  
  3. @NgModule({
  4. declarations: [
  5. AppComponent,
  6. HeapMemoryComponent,
  7. DiskSpaceComponent,
  8. CpuUsageComponent,
  9. FreeMemoryComponent,
  10. BottomPanelDynamicComponent
  11. ],
  12. imports: [
  13. BrowserModule,
  14. FormsModule,
  15. HttpModule,
  16. MaterialModule.forRoot(),
  17. DndModule.forRoot(),
  18. routing
  19. ],
  20. providers: [.....],
  21. bootstrap: [AppComponent],
  22. entryComponents:[...],
  23. exports: [...]
  24. })
  25. export class AppModule { }
  26.  
  27. import { Component, OnInit } from '@angular/core';
  28. import {HeapMemoryComponent} from '../heap-memory-graph/heap-memory-graph.component';
  29. import {DiskSpaceComponent} from '../disk-space-graph/disk-space-graph.component';
  30. import {CpuUsageComponent} from '../cpu-usage-graph/cpu-usage-graph.component';
  31. import {FreeMemoryComponent} from '../free-memory-graph/free-memory-graph.component';
  32.  
  33. @Component({
  34. selector: 'app-rightpanel',
  35. templateUrl: './rightpanel.component.html',
  36. styleUrls: ['./rightpanel.component.css']
  37. })
  38. export class RightpanelComponent implements OnInit {
  39. componentData2 = null;
  40. constructor(private savedWidgetService:GetSavedWidgetService) {}
  41.  
  42. ngOnInit() {
  43. this.service.getSavedWidget().subscribe(lst => {
  44. for (var v in lst) {
  45. if (lst[v].name == "Heap Memory") {
  46. this.componentData2 = {
  47. component: HeapMemoryComponent,
  48. inputs: {
  49. showNum: 0
  50. }
  51. };
  52. } else if (lst[v].name == "Disk Space") {
  53. this.componentData2 = {
  54. component: DiskSpaceComponent,
  55. inputs: {
  56. showNum: 0
  57. }
  58. };
  59. } else if (lst[v].name == "CPU Usage") {
  60. this.componentData2 = {
  61. component: CpuUsageComponent,
  62. inputs: {
  63. showNum: 0
  64. }
  65. };
  66. } else if (lst[v].name == "Free Memory") {
  67. this.componentData2 = {
  68. component: FreeMemoryComponent,
  69. inputs: {
  70. showNum: 0
  71. }
  72. };
  73.  
  74. }
  75. }
  76. }
  77. }
  78. }
  79.  
  80. <app-bottompanel [componentData2]="componentData2" class="row"></app-bottompanel>
  81.  
  82. import {Component, Input, ViewContainerRef, ViewChild, ReflectiveInjector, ComponentFactoryResolver} from '@angular/core';
  83. import {HeapMemoryComponent} from '../heap-memory-graph/heap-memory.component';
  84. import {DiskSpaceComponent} from '../disk-space-graph/disk-space.component';
  85. import {CpuUsageComponent} from '../cpu-usage-graph/cpu-usage.component';
  86. import {FreeMemoryComponent} from '../free-memory-graph/free-memory.component';
  87.  
  88. @Component({
  89. selector: 'app-bottompanel',
  90. entryComponents[HeapMemoryComponent,DiskSpaceComponent,CpuUsageComponent,FreeMemoryComponent],
  91. template: `<div #dynamicComponentContainer></div>`,
  92. })
  93. export default class BottomPanelDynamicComponent {
  94. currentComponent2 = null;
  95.  
  96. @ViewChild('dynamicComponentContainer', { read: ViewContainerRef })
  97. dynamicComponentContainer: ViewContainerRef;
  98.  
  99. // component: Class for the component you want to create
  100. // inputs: An object with key/value pairs mapped to input name/input value
  101. @Input() set componentData2(data: {component: any, inputs: any }) {
  102. if (!data) {
  103. return;
  104. }
  105.  
  106. // Inputs need to be in the following format to be resolved properly
  107. let inputProviders = Object.keys(data.inputs).map((inputName) => {return {provide: inputName, useValue: data.inputs[inputName]};});
  108. let resolvedInputs = ReflectiveInjector.resolve(inputProviders);
  109.  
  110. // We create an injector out of the data we want to pass down and this components injector
  111. let injector = ReflectiveInjector.fromResolvedProviders(resolvedInputs, this.dynamicComponentContainer.parentInjector);
  112.  
  113. // We create a factory out of the component we want to create
  114. let factory = this.resolver.resolveComponentFactory(data.component);
  115.  
  116. // We create the component using the factory and the injector
  117. let component = factory.create(injector);
  118.  
  119. // We insert the component into the dom container
  120. this.dynamicComponentContainer.insert(component.hostView);
  121.  
  122. // We can destroy the old component is we like by calling destroy
  123. if (this.currentComponent2) {
  124. //this.currentComponent.destroy();
  125. }
  126.  
  127. this.currentComponent2 = component;
  128. }
  129.  
  130. constructor(private resolver: ComponentFactoryResolver) {
  131.  
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement