如何从一个组件到另一个组件以角度打开模态? [英] How open modal from one component into another component in angular?

查看:74
本文介绍了如何从一个组件到另一个组件以角度打开模态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建模态模态组件并将模态编码在modal.component.html文件中.我想在标题组件的header.component.html文件中使用此模式.

i create modal modal component and code my modals in modal.component.html file. i want use this modal in my header.component.html file in header component.

相关部分如下:

<div style="text-align: center">
  <a class="social-account-profile icon-plus"
     (click)="onButtonClick()"</a>
</div>

onButtonClick 是我的header.component.ts中的一种方法,如下所示:

onButtonClick is a method in my header.component.ts like below:

export class HeaderComponent implements OnInit {

  constructor(public router: Router, public authService: AuthService,
              public appGlobal: AppGlobals, public  modal: ModalComponent) {
  }

  ngOnInit() {
  }

  onButtonClick() {
    this.modal.openModal();
  }

} 

openModal 是我的modal.component.ts文件中的以下方法:

openModal is below method in my modal.component.ts file :

export class ModalComponent implements OnInit {

  @ViewChild('projctModal') projctModal: any;
  openModal() {
    this.projctModal.show();
  }

  constructor(private formvalidation: FormValidation) {
  }

  ngOnInit() {
  }

}

和我的modal.component.html文件在下面:

and my modal.component.html file is below:

<div mdbModal #projctModal="mdbModal" class="modal fade top " id="projctModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel"
     aria-hidden="true">
</div>

运行代码时,我在Chrome控制台中收到此错误:

when i run code i get this error in my chrome console:

ERROR TypeError: Cannot read property 'show' of undefined

我该如何解决?

推荐答案

您需要创建一个模式服务以使其在下面的示例示例中工作.

You need to create a modal service to make it work sample example below.

ModalComponent

ModalComponent

import { Component, ElementRef, Input, OnInit,ViewChild, OnDestroy } from '@angular/core';
import {ModalService} from './modal.service';

@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html'
})
export class ModalComponent implements OnInit, OnDestroy {
  @Input() id: string;
    private element: any;
     @ViewChild('basicModal') basicModal: ElementRef;


    constructor(private modalService: ModalService, private el: ElementRef) {
        this.element = el.nativeElement;
    }

    ngOnInit(): void {
        let modal = this;

        // ensure id attribute exists
        if (!this.id) {
            console.error('modal must have an id');
            return;
        }
        // add self (this modal instance) to the modal service so it's accessible from controllers
        this.modalService.add(this);
    }

    // remove self from modal service when directive is destroyed
    ngOnDestroy(): void {
        this.modalService.remove(this.id);
        this.element.remove();
    }

    // open modal
    open(): void {
        this.basicModal.show();
    }

    // close modal
    close(): void {
        this.basicModal.hide()
    }


}

它是html

<div mdbModal #basicModal="mdbModal" class="modal fade right" tabindex="-1" role="dialog" aria-labelledby="myBasicModalLabel"
  aria-hidden="true">
  <div class="modal-dialog modal-full-height modal-right" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title w-100" id="myModalLabel">Modal title</h4>

        <button type="button" class="close pull-right" aria-label="Close" (click)="basicModal.hide()">
          <span aria-hidden="true">×</span>
        </button>
       </div>
      <div class="modal-body">
         <ng-content></ng-content>
      </div>
      <div class="modal-footer">
        <button type="button" mdbBtn color="secondary" class="waves-light" aria-label="Close" (click)="basicModal.hide()" mdbWavesEffect>Close</button>
        <button type="button" mdbBtn color="primary" class="relative waves-light" mdbWavesEffect>Save!</button>
      </div>
    </div>
  </div>
</div>

模态服务代码如下

import { Injectable } from '@angular/core';

@Injectable()
export class ModalService {

  private modals: any[] = [];

     add(modal: any) {
        this.modals.push(modal);
    }

    remove(id: string) {
        this.modals = this.modals.filter(x => x.id !== id);
    }

    open(id: string) {
        let modal: any = this.modals.filter(x => x.id === id)[0];
        modal.open();
    }

    close(id: string) {
        let modal: any = this.modals.filter(x => x.id === id)[0];
        modal.close();
    }

}

在您的实际组件中使用并像这样

ANd in your actual component use it like this

import { Component } from '@angular/core';

import {ModalService} from './modal/modal.service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
   private bodyText: string;

    constructor(private modalService: ModalService) {
    }

    ngOnInit() {
        this.bodyText = 'This text can be updated in modal 1';
    }

    openModal(id: string) {
        this.modalService.open(id);
    }

    closeModal(id: string) {
        this.modalService.close(id);
    }


}

及其html

<div class="col-md-6 col-md-offset-3">
    <h1>Home</h1>
    <p>{{bodyText}}</p>
    <button (click)="openModal('custom-modal-1')">Open Modal 1</button>
    <button (click)="openModal('custom-modal-2')">Open Modal 2</button>

</div>


<app-modal id="custom-modal-1">
    <h1>A Custom Modal!</h1>
    <p>Home page text: <input type="text" [(ngModel)]="bodyText" /></p>
    <button (click)="closeModal('custom-modal-1');">Close</button>
</app-modal>

<app-modal id="custom-modal-2">
    <h1>A Custom Modal!</h1>

</app-modal>

这篇关于如何从一个组件到另一个组件以角度打开模态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆