Angular 2 - 打开/关闭默认引导模式

Iva*_*ers 18 typescript bootstrap-modal angular

我不想在问题/答案中建议使用angular2-bootstrapng2-bs3-modal Angular 2 Bootstrap ModalAngular 2.0 and Modal Dialog

现在.我知道什么打开和关闭引导模式.

  • 显示屏在display: none;和之间切换display: block;
  • 一个属性可打开,div之间aria-hidden="true"aria-hidden="false

所以,我自然认为如果我这样绑定aria-hidden属性[aria-hidden]="true",我就可以操纵它.可悲的是,我不能约束,aria-hidden因为它不是一个已知的属性div.(注意,attr.aria-hidden不存在)

我知道这可以用JQuery实现$('#myModal').modal()如下问题所示如何使用jQuery打开一个Bootstrap模式窗口?

所以我的问题是,是否有一个TypeScript功能modal()与JQuery 做同样的事情,或者有没有办法将函数/变量绑定到aria-hidden

我目前html:

<div id="createAccountModal" class="modal fade customForm" role="dialog" [aria-hidden]="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4>Create account</h4>
            </div>
            <div class="modal-body">
               <p>Lorem ipsum</P>
            </div>
            <div class="modal-footer align-left">
                My custom footer
            </div>
        </div>
    </div>
</div
Run Code Online (Sandbox Code Playgroud)

Avn*_*kya 16

你可以试试这样的东西,创造myModal.html:

<div class="modal-backdrop fade in" [style.display]="showModal ? 'block' : 'none'"></div>
  <div class="modal" tabindex="-1" role="dialog" style="display: block" [style.display]="showModal ? 'block' : 'none'">
     <div class="modal-dialog">
        <div class="modal-popup">
          <div class="popup-title">
            <span>{{title}} </span>
            <i class="icon-cancel fr" data-dismiss="modal" aria-label="Close" (click)="cancelAction()"></i>
            <p *ngIf="subTitle">{{subTitle}}</p>
          </div>
        <ng-content></ng-content>
      </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

然后,创建myModal.component.ts:

import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core';

const template: string = require('./myModal.html');

@Component({
   selector: 'modal',
   template
})

export class Modal implements OnInit {
  @Input('show-modal') showModal: boolean;
  @Input('title') title: string;
  @Input('sub-title') subTitle: string;
  @Input('cancel-label') cancelLabel: string;
  @Input('positive-label') positiveLabel: string;

  @Output('closed') closeEmitter: EventEmitter < ModalResult > = new EventEmitter < ModalResult > ();
  @Output('loaded') loadedEmitter: EventEmitter < Modal > = new EventEmitter < Modal > ();
  @Output() positiveLabelAction = new EventEmitter();

  constructor() {}

  ngOnInit() {
    this.loadedEmitter.next(this);
  }

  show() {
    this.showModal = true;
  }

  hide() {
    this.showModal = false;
    this.closeEmitter.next({
      action: ModalAction.POSITIVE
    });
  }

  positiveAction() {
    this.positiveLabelAction.next(this);
    return false;
  }

  cancelAction() {
    this.showModal = false;
    this.closeEmitter.next({
      action: ModalAction.CANCEL
    });
    return false;
  }
}

export enum ModalAction { POSITIVE, CANCEL }

export interface ModalResult {
  action: ModalAction;
}
Run Code Online (Sandbox Code Playgroud)

然后为此创建模块,以便您可以在任何地方使用并在任何地方使用它,如下所示:

<modal #deleteUserModal id="deleteUser"
   [show-modal]="isModalOpen"
   [title]="'Delete'"
   >
  <div class="popup-content">
    <p>Are you sure you want to delete the user permanently?</p>
  </div>
  <div class="popup-footer">
    <button class="btn cancel"  aria-label="Close" (click)="deleteUserModal.hide()">
        Cancel
    </button>
    <button type="button" class="btn btn-primary" (click)="deleteSelectedUser(deleteUserModal)" aria-label="Close">
        Delete
    </button>
   </div>
 </modal>
Run Code Online (Sandbox Code Playgroud)

你也可以增强这个:)


Fer*_*ira 8

如果你的模态有一个取消按钮(否则创建一个隐藏的关闭按钮).您可以在此按钮上模拟单击事件,以便关闭表单.在你的组件中添加一个ViewChild

export class HelloComponent implements OnInit {

@ViewChild('fileInput') fileInput:ElementRef;
Run Code Online (Sandbox Code Playgroud)

在关闭按钮中添加#fileInput

<button class="btn btn-danger" #fileInput id="delete-node" name="button" data-dismiss="modal" type="submit">Cancelar</button>
Run Code Online (Sandbox Code Playgroud)

如果要以编程方式关闭模式,请在关闭按钮上触发单击事件

this.fileInput.nativeElement.click();
Run Code Online (Sandbox Code Playgroud)

打开你可以使用相同的想法


小智 5

我这样做了,它对我很有用.

var element = document.getElementById("CloseButton")as any;

element.click(); 我的CloseButton是bootstrap关闭按钮