簡體   English   中英

ng2-modal-module:成功事件時傳遞數據

[英]ng2-modal-module : passing data on success events

這就是我要去的地方。 不能弄清楚如何通過成功事件發送我的表格。 那有可能嗎? 因為我想將用戶的詳細信息保存在MongoDB中,所以在這種情況下如何獲取它們。 過去約10個小時一直停留在它上面

app.component.html

    <button class="btn btn-success" (click)="displayModal()">Add</button>
    <ng2-modal-window id="addUserModal"></ng2-modal-window>

app.component.ts

    displayModal(){

    let successEventName = 'successEvent'; 
    let cancelEventName = 'cancelEvent';

    this.modal.resetEventsSubscribers([successEventName, cancelEventName]);

    this.modal.showModal("addUserModal",{
        title: 'Add User',
        htmlContent: `
            <form [formGroup]="myForm">
                <div class="form-group">
                    <label for="firstName">First Name</label>
                    <input type="text" id="firstname" class="form-control" formControlName="firstname" #fname>
                </div>
                <div class="form-group">
                    <label for="lastName">Last Name</label>
                    <input type="text" id="lastname" class="form-control" formControlName="lastname">
                </div>
                <div class="form-group">
                    <label for="firstName">Email</label>
                    <input type="text" id="email" class="form-control" formControlName="email">
                </div>
            </form>
        `,
        buttons: {
            success: { event: successEventName },
            cancel: { event: cancelEventName }    
        }
    });

    this.pubsub.subscribe(successEventName, (data) => {
        console.log('Success triggered!', data); 
    });

    this.pubsub.subscribe(cancelEventName, (data) => {  
        console.log('cancelEventName triggered!', data);  
    });
}

我對ng2-modal-module不熟悉,但是假設您知道如何接收成功事件,其余的工作應該很簡單。 由於您使用反應式表單API,因此應在某處(代碼示例中未顯示)創建myForm實例。

變量myForm保存在表單中輸入的數據,您可以使用this.myForm.value訪問整個對象。 如果要訪問特定表單控件的值,可以這樣進行:

this.myForm.value.firstName

然后將此值與Http或HttpClient對象一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM