Пытаюсь закрыть одно модальное окно и открыть другое, но оба продолжают закрываться

Я использую angular 4, bootstrap 4 (бета) и ngx-bootstrap. Я пытаюсь создать модальную службу, которая будет служить диалоговым окном предупреждения на любой странице. Проблема в том, что я пытаюсь закрыть модальное test и открыть модальное alert, но alertModal не открывается, я думаю, потому что this.bsModalRef.hide скрывает их обоих.

Что я делаю не так?

test.html

<div bsModal #newBidModal="bs-modal"  tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog w-100">
        <div class="modal-content w-100">
            <form class="form-group" [formGroup]="testForm" (ngSubmit)="onSubmit(testForm.value)">
            </form
        </div>
    </div>
</div>


<app-modal  #childModal [title]="'Modal'">
  <div class="modal-body">
    {{message}}
  </div>
</app-modal>

test.component.ts

export class TestComponent implements OnInit {
  TestComponent: any;

  @ViewChild('childModal') childModal: ModalComponent;
  message: string;

  constructor(public bsModalRef: BsModalRef){}

  onSubmit(input:any) {
    this.API.post(input).subscribe(data => {
      this.bsModalRef.hide();
      this.alert();
    });
  }
}

alert() {
    this.childModal.title = 'hi';
    this.message = 'test';
    this.childModal.show();
}

alert-modal.html

<div bsModal #childModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title pull-left">{{title}}</h4>
        <button type="button" class="close pull-right" (click)="childModal.hide()" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <ng-content select=".modal-body"> </ng-content>
      </div>
      <div class="modal-footer">
        <div class="pull-left">
          <button class="btn btn-default" (click)="childModal.hide()"> Cancel </button>
        </div>
      </div>
    </div>
  </div>
</div>

alert-modal.ts

export class AlertModalComponent implements OnInit {

  @ViewChild('childModal') public childModal: ModalDirective;

  @Input() title: string;
  constructor() {
  }
  show() {
    this.childModal.show();
  }
  hide() {
    this.childModal.hide();
  }
}

person derrickrozay    schedule 19.10.2017    source источник
comment
Не могли бы вы создать plunkr с воспроизведением этой проблемы? Начальные шаблоны — Plunkr: plnkr.co/edit/0NipkZrnckZZROAcnjzB?p=preview StackBlitz: stackblitz.com/edit/   -  person IlyaSurmay    schedule 20.10.2017


Ответы (1)


Я не работаю с angular js и не использую «ngx-bootstrap», но если вы хотите добиться «закрыть один модальный и открыть другой», вы можете сделать это в начальной загрузке, добавив data-toggle и data-target к кнопкам первого модального окна, чтобы открыть следующий модальный при закрытии.

Скажем, у вас есть модальная кнопка:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Launch First Modal
</button>

Что нацелено на это модальное окно:

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
   <!-- rest of modal content here, closing button targets exampleModal2 -->
    <button type="button" class="close" data-dismiss="modal" aria-label="Close" data-toggle="modal" data-target="#exampleModal2">
    <span aria-hidden="true">&times;</span>
    </button>
  </div>
</div>

Второй модал здесь с идентификатором exampleModal2 будет открыт, если первый закрыт.

<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel2" aria-hidden="true">
  <!-- Modal content -->
</div>

См. скрипку здесь

person Abdoulie Kassama    schedule 20.10.2017
comment
Вы можете сделать это через якорную ссылку для модального окна, чтобы открыть другое модальное окно? Я не хочу открывать другое, просто закрывая другое модальное окно. Я хочу выбрать ссылку из первого модального окна, чтобы открыть второе модальное окно и, таким образом, закрыть первое модальное окно. - person Krys; 05.04.2018
comment
@Krys Да, вы можете, просто добавьте к ссылке атрибуты data-dismiss и data-target, например: <a data-dismiss="modal" data-toggle="modal" data-target="#secondModal">Link</a> - person Abdoulie Kassama; 05.04.2018