使用angular 2中的typescript关闭bootstrap模态

use*_*630 6 typescript angular

我有一个按钮,点击它我打开一个bootstrap模态弹出窗口.模态弹出窗口包含一些带有提交按钮的字段.我想在保存数据时关闭弹出窗口.我无法使用数据解除,因为它会在用户点击按钮后立即关闭弹出窗口.有没有办法通过打字稿关闭弹出窗口?

expense.component.html

<div id="AddExpense" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Add Expense</h4>
                </div>
                <div class="modal-body">
                    <form id="form" (ngSubmit)="saveExpense();">
                        <div class="form-group">
                            <table class="table table-responsive" style="border:0">
                                <tr *ngFor="#column of columnInputs" style="height:20px;">
                                    <td class="text-right" style="padding-top:10px;border:0">
                                        <h4> {{column.name | case}}: </h4>
                                    </td>
                                    <td class="text-center" style="padding-top:10px;border:0">
                                        <input *ngIf="column.name != 'status'" type="{{column.name == 'created_Date' ? 'date' : 'text'}}" name="{{columns.name}}" required [(ngModel)]="column.value" class="form-control" />
                                        <select class="form-control" *ngIf="column.name == 'status'" [(ngModel)]="column.value" name="{{column.name}}" required>
                                            <option value="status">--Select--</option>
                                            <option value="1">Paid</option>
                                            <option value="2">Unpaid</option>
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary btn-lg"> Add Expense </button>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                </div>
            </div>
        </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

小智 8

你可以使用关闭按钮上的操作

<div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" #closeAddExpenseModal>&times;</button>
                <h4 class="modal-title">Add Expense</h4>
            </div>
Run Code Online (Sandbox Code Playgroud)

并且在您的控制器中,您可以在使用的操作后添加此行

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

您需要将此导入添加到控制器

import { ViewChild, ElementRef} from '@angular/core';
Run Code Online (Sandbox Code Playgroud)

您还需要定义closeAddExpenseModal

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

  • 我相信这应该是公认的答案,因为它展示了 OP 正在寻求的解决方案。另外,在我看来,它是解决这个问题的侵入性最小的解决方案(即它不需要混合使用 Typescript 和 Javascript)。 (2认同)

Mar*_*ano -3

我不确定你真正需要做什么,但如果你想用打字稿关闭模态,你可以给你的 html 模态提供一个 id 并调用“隐藏”方法。

$('#newPostModal').modal('hide');    
Run Code Online (Sandbox Code Playgroud)

抱歉,如果这不是您想要的,也许您能更好地解释我,我可以帮助您

  • 只需导入“声明 var $: any;” (2认同)