如何动态创建自举模式作为Angular2组件?

eko*_*eko 16 angular

原始标题:无法在Angular 2中初始化动态追加(HTML)组件

我已经创建了一个指令,在初始化时将一个模态附加到正文.当单击一个按钮(带有指令注入其中)时,这个模态会激活.但我希望这个模态的内容是另一个组件(实际上我希望模态是组件).似乎我无法初始化组件.

这是我所做的一切:

http://plnkr.co/edit/vEFCnVjGvMiJqb2Meprr?p=preview

我正在尝试将my-comp作为我的组件的模板

 '<div class="modal-body" #theBody>' 
            + '<my-comp></my-comp>' + 
 '</div>
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 12

更新2.0.0决赛

Plunker示例> = 2.0.0

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, ModalComponent, CompComponent],
  providers: [SharedService],
  entryComponents: [CompComponent],
  bootstrap: [ App, ModalComponent ]
})
export class AppModule{}
Run Code Online (Sandbox Code Playgroud)
export class ModalComponent {
  @ViewChild('theBody', {read: ViewContainerRef}) theBody;

  cmp:ComponentRef;

  constructor(
    sharedService:SharedService, 
    private componentFactoryResolver: ComponentFactoryResolver, 
    injector: Injector) {

    sharedService.showModal.subscribe(type => {
      if(this.cmp) {
        this.cmp.destroy();
      }
      let factory = this.componentFactoryResolver.resolveComponentFactory(type);
      this.cmpRef = this.theBody.createComponent(factory)
      $('#theModal').modal('show');
    });
  }

  close() {
    if(this.cmp) {
      this.cmp.destroy();
    }
    this.cmp = null;
  }
}
Run Code Online (Sandbox Code Playgroud)

暗示

如果一个应用程序改变状态SharedService或调用导致Observable发出值的方法,并且订阅者在发射器的不同应用程序中,则订户中的代码在发射器中执行NgZone.

因此在订阅SharedService使用中的观察时

class MyComponent {
  constructor(private zone:NgZone, private sharedService:SharedService) {
    private sharedService.subscribe(data => this.zone.run() => {
      // event handler code here
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

有关如何触发更改检测的更多详细信息,请参阅手动触发Angular2更改检测

原版的

动态添加的HTML不由Angular处理,也不会导致实例化或添加组件或指令.

您不能使用DynamicComponentLoader(不建议使用)Angular 2根组件(AppComponent)之外的组件添加组件ViewContainerRef.createComponent()(Angular 2动态选项卡,用户单击所选组件).

我想最好的方法是在Angulars之外创建第二个组件root组件是调用bootstrap()每个组件并使用共享服务进行通信:

var sharedService = new SharedService();

bootstrap(AppComponent, [provide(SharedService, {useValue: sharedService})]);
bootstrap(ModalComponent, [provide(SharedService, {useValue: sharedService})]);
Run Code Online (Sandbox Code Playgroud)

Plunker示例beta.17
Plunker示例beta.14

@Injectable()
export class SharedService {
  showModal:Subject = new Subject();
}

@Component({
  selector: 'comp-comp',
  template: `MyComponent`
})
export class CompComponent { }


@Component({
  selector: 'modal-comp',
  template: `
  <div class="modal fade" id="theModal" tabindex="-1" role="dialog" aria-labelledby="theModalLabel">
    <div class="modal-dialog largeWidth" role="document">
      <div class="modal-content">
        <div class="modal-header">
        <h4 class="modal-title" id="theModalLabel">The Label</h4></div>
        <div class="modal-body" #theBody>
      </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal" (close)="close()">Close</button>
  </div></div></div></div>
`
})
export class ModalComponent {
  cmp:ComponentRef;
  constructor(sharedService:SharedService, dcl: DynamicComponentLoader, injector: Injector, elementRef: ElementRef) {
    sharedService.showModal.subscribe(type => {
      if(this.cmp) {
        this.cmp.dispose();
      }
      dcl.loadIntoLocation(type, elementRef, 'theBody')
      .then(cmp => {
        this.cmp = cmp;
        $('#theModal').modal('show');
      });
    });
  }

  close() {
    if(this.cmp) {
      this.cmp.dispose();
    }
    this.cmp = null;
  }
}


@Component({
  selector: 'my-app',
  template: `
<h1>My First Attribute Directive</h1>
<button (click)="showDialog()">show modal</button>
<br>
<br>`,
})
export class AppComponent {
  constructor(private sharedService:SharedService) {}

  showDialog() {
    this.sharedService.showModal.next(CompComponent);
  }
}
Run Code Online (Sandbox Code Playgroud)