Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. <razor>
  2. <mvc-partial>
  3. <dynamic-html> // buttonPress(){console.log("Function called in dynamicHtml)
  4. // Output() to call function in razor.ts
  5. </dynamic-html>
  6. </mvc-partial>
  7. <razor>
  8.  
  9. import {
  10. Component,
  11. Directive,
  12. NgModule,
  13. Input,
  14. Output,
  15. EventEmitter,
  16. ViewContainerRef,
  17. Compiler,
  18. ComponentFactory,
  19. ModuleWithComponentFactories,
  20. ComponentRef,
  21. ReflectiveInjector, OnInit, OnDestroy, ViewChild
  22. } from '@angular/core';
  23.  
  24. import { RouterModule } from '@angular/router';
  25. import { CommonModule } from '@angular/common';
  26. import { Http } from "@angular/http";
  27. import 'rxjs/add/operator/map';
  28.  
  29. export function createComponentFactory(compiler: Compiler, metadata: Component): Promise<ComponentFactory<any> | undefined>{
  30. console.log(compiler)
  31. console.log(metadata)
  32. class DynamicComponent {
  33. @Output() buttonType: EventEmitter<string> = new EventEmitter<string>()
  34.  
  35. // button click operation
  36. buttonPress() {
  37. this.buttonType.emit();
  38. }
  39. };
  40. const decoratedCmp = Component(metadata)(DynamicComponent);
  41.  
  42. @NgModule({ imports: [CommonModule, RouterModule], declarations: [decoratedCmp] })
  43. class DynamicHtmlModule { }
  44.  
  45. return compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule)
  46. .then((moduleWithComponentFactory: ModuleWithComponentFactories<any>) => {
  47. console.log(decoratedCmp)
  48. console.log(moduleWithComponentFactory.componentFactories.find(x => x.componentType === decoratedCmp))
  49. return moduleWithComponentFactory.componentFactories.find(x => x.componentType === decoratedCmp);
  50. });
  51. }
  52.  
  53. @Component({
  54. selector: 'mvc-partial',
  55. template: `<div #dynamicHtml></div>`
  56. })
  57. //@Directive({ selector: 'mvc-partial' })
  58. export class RenderingViewDynamic implements OnInit {
  59. @ViewChild('dynamicHtml', { read: ViewContainerRef }) target: ViewContainerRef;
  60. html: string = '<p></p>';
  61. @Input() url: string;
  62. cmpRef: ComponentRef<any>;
  63.  
  64. constructor(private vcRef: ViewContainerRef, private compiler: Compiler, private http: Http) { }
  65. clicks() {
  66. console.log("HI")
  67. }
  68. ngOnInit() {
  69. this.http.get(this.url)
  70. .map(res => res.text())
  71. .subscribe(
  72. (html) => {
  73. this.html = html;
  74. if (!html) return;
  75.  
  76. if (this.cmpRef) {
  77. this.cmpRef.destroy();
  78. }
  79.  
  80. const compMetadata = new Component({
  81. selector: 'dynamic-html',
  82. template: this.html,
  83. });
  84.  
  85. createComponentFactory(this.compiler,compMetadata)
  86. .then(factory => {
  87. //const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
  88. this.cmpRef = this.target.createComponent(factory!);
  89. });
  90. },
  91. err => console.log(err),
  92. () => console.log('MvcPartial complete')
  93. );
  94.  
  95. }
  96.  
  97. ngOnDestroy() {
  98. if (this.cmpRef) {
  99. this.cmpRef.destroy();
  100. }
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement