1
/* COMPONENT */ 
import { Modal } from './../../common-components/modal/modal.model'; 
import { Buttons } from './playground.model'; 
import { Component, OnInit } from '@angular/core'; 
import template from "./playground.component.html"; 
import style from "./playground.component.scss"; 

declare var jQuery: any; 
@Component({ 
    selector: 'app-playground', 
    template, 
    styles: [style] 
}) 

export class PlaygroundComponent implements OnInit { 
    vm = new Modal({ 
     id: 'vid-modal', 
     customColor: '#F00', 
     showHeader: true, 
     showClose: true, 
     dismissible: false 
    }); 
    buttons = Buttons; 
    constructor() {} 
    ngOnInit() { 
    } 
    ngAfterViewInit() { 
     this.vm.actions.emit({ action: 'modal', params: ['open']}); 
    } 
    modalReady() { 
     this.vm.actions.emit({ action: 'modal', params: ['open']}); 
    } 
    openModal() { 
     this.vm.actions.emit({ action: 'modal', params: ['open']}); 
    } 

} 

/* HTML */ 
<vc-button [button]="buttons.save_btn" (click)="openModal()"></vc-button> 

<vc-modal [modal]="vm" (modalReady)="modalReady()"> 
    <vc-modal-body> 
     IS IT WORKING? 
    </vc-modal-body> 
</vc-modal> 

我试图打开一个模式,只要使用ngAfterViewInit初始化页面。 我也在模态组件中使用了一个发射器,它告诉我模态已经初始化了。 即使在这一切之后,我无法打开关于初始化的模式。 它只适用于点击按钮。 我不想使用任何jQuery或第三方解决方案。我知道这是一个异步问题,但我无法使用observables/replay主题来解决它。无法打开模式初始化angular2

+0

组件的类必须实现AfterViewInit: 出口类PlaygroundComponent实现的OnInit,AfterViewInit { – Faly

+0

@Faly,这是没有必要的,角认识到这些不必说'工具。 ..' :)这些标记更支持开发人员(和IDE) – Alex

+0

是的,没有必要的工具。但我很确定问题在于动作,因为它是一个正常的主题,而不是一个重播主题,它没有按时初始化。 –

回答

0

试试这个:

export class PlaygroundComponent implements OnInit { 
... 
    ngOnInit(){ 
    setTimeout(() => { 
     this.vm.actions.emit({ action: 'modal', params: ['open']}); 
    }, 1); 
    } 
... 
} 
相关问题