简体   繁体   English

如何在angular 2引导程序模式中使用* ngFor

[英]How to use *ngFor in angular 2 bootstrap modal

I am trying to bind data to my modal using *ngFor in angular 2 but not able to view any data in modal when in my component data is available. 我试图在angular 2中使用* ngFor将数据绑定到我的模态,但是当我的组件数据可用时,无法查看模态中的任何数据。

modal.component.html modal.component.html

 <div class="modal modalscroll" tabindex="-1" role="dialog"  [ngStyle]="{'display':addSplitBilling}">
    <div class="modal-dialog splitBillingModal">
    <form>
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true" (click) = "clearSplitBilling()">×</button>
          <h4 class="modal-title">Split Billing</h4>
        </div>
        <div class="container"></div>
        <div class="modal-body">
          <table>
                <tbody>
                    <tr *ngFor="let d of data.dupStmtAddresses; let i = index">
                          <td>{{d.addressee}}</td>
                          <td>{{d.splitPercent}}</td>
                      </tr>
                </tbody>
            </table>
        </div>
        <div class="modal-footer">
        </div>

      </div>
    </form>
    </div>
  </div>

modal.component.ts modal.component.ts

export class DupStatementsComponent implements OnInit {
    addSplitBilling = 'none';
    data: any;

    ngOnInit() {
        this.getData();
    }


    getData(){
        this.duplicateStatementsProvider.getDuplicateStatements()
        .subscribe(
            data => {
                this.data = JSON.parse(data['_body']);
            },
            error => {
            }
        );
    }


    splitBilling(){
        console.log(this.data.dupStmtAddresses);
        this.addSplitBilling = 'block';
    }
}

In console I can see that data is available but in modal not able to view any data. 在控制台中,我可以看到数据可用,但在模式下无法查看任何数据。

这是我在控制台中得到的

In data i am getting this response from web service --> 在数据中,我从Web服务获得此响应->

{"createdDate":"2017-12-04T17:04:00.174+05:30","id":1,"dupStmtAddresses":[{"createdDate":null,"id":1,"addressee":"a","address1":"a","address2":"a","address3":"a","address4":"a","email":"a","splitPercent":null,"new":false},{"createdDate":null,"id":2,"addressee":"b","address1":"b","address2":"b","address3":"b","address4":"b","email":"b","splitPercent":null,"new":false},{"createdDate":null,"id":3,"addressee":"c","address1":"c","address2":"c","address3":"c","address4":"c","email":"c","splitPercent":null,"new":false}],"enableSplitBilling":true,"new":false}

I have no idea why it is not working. 我不知道为什么它不起作用。

What I can conclude from your description and comments above is that, the service is yet to respond before the modal template is created. 从上面的描述和评论中可以得出的结论是,在创建模态模板之前,该服务尚未响应。 I am not sure if it will work but could you please try: 我不确定是否可以使用,但请您尝试:

Instead of using data directly in modal, use a new variable modalContent initialised to null/empty string. 代替直接在模式中使用数据,可以使用初始化为null / empty字符串的新变量modalContent。 On click of button to open modal, assign the data to modalContent variable and use this in ngFor loop. 单击按钮以打开模态时,将数据分配给modalContent变量,并在ngFor循环中使用它。

export class DupStatementsComponent implements OnInit {
    modalContent: any = '';
    //... same as your logic

    splitBilling(){
        console.log(this.data.dupStmtAddresses);
        this.modalContent = this.data.dupStmtAddresses;
        this.addSplitBilling = 'block';
    }
}

your ngFor loop: 您的ngFor循环:

<tr *ngFor="let d of modalContent; let i = index">

Kindly share if it works! 如果可以,请分享!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM