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.
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
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 -->
{"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. On click of button to open modal, assign the data to modalContent variable and use this in ngFor
loop.
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:
<tr *ngFor="let d of modalContent; let i = index">
Kindly share if it works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.