我正在寻找在我的角度4应用程序中切换三个div。我基本上有以下三个div 收入证明 现金流量表 资产负债表
如果您注意到ul-li标签内的锚标签。单击锚标记应显示相应的div。我已经将href设置为href =“javascript:void(0);并且尝试基于showTable变量进行切换,默认情况下,该变量在组件代码上设置为true。由于某种原因它不起作用。有人能告诉我我怎么能去实现它
<div class="card-body">
<ul class="nav nav-pills financial-tab" id="financial-tab" role="tablist">
<li class="nav-item">
<a class="nav-link active" [ngClass]="showTable ? '' : 'active' " id="sincome-tab" data-toggle="pill" href="javascript:void(0);" role="tab" aria-controls="table"
aria-selected="true" (click)="showTable = !showTable">Income Statement</a>
</li>
<li class="nav-item">
<a class="nav-link" [ngClass]="!showTable ? '' : 'active' " id="cash-tab" data-toggle="pill" href="javascript:void(0);" role="tab" aria-controls="chart"
aria-selected="false" (click)="showTable = !showTable">Cash Flow Statement</a>
</li>
<li class="nav-item">
<a class="nav-link" [ngClass]="!showTable ? '' : 'active' " id="balance-tab" data-toggle="pill" href="javascript:void(0);" role="tab" aria-controls="chart"
aria-selected="false" (click)="showTable = !showTable">Balance Sheet</a>
</li>
</ul>
<div class="tab-content Financial-content" id="pills-tabContent">
<!-- Income table -->
<div *ngIf="showTable" class="tab-pane fade active show" id="base-strategy--fs-statement" role="tabpanel" aria-labelledby="table-tab" aria-expanded="false">
<table class="table">
<thead>
<tr>
<th></th>
<th>2017</th>
<th>2018</th>
<th>2019</th>
<th>2020</th>
<th>2021</th>
</tr>
</thead>
<tbody>
<tr>
<td>Direct premiums written</td>
<td>33,150,000</td>
<td>33,813,000</td>
<td>35,179,045 </td>
<td>35,882,626 </td>
<td>35,882,626 </td>
</tr>
<tr>
<td>Assumed premiums written</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
</div>
<div *ngIf="showTable" class="tab-pane fade" id="base-strategy--fs-cashflow" role="tabpanel" aria-labelledby="table-tab" aria-expanded="false">
<table class="table">
<thead>
<tr>
<th></th>
<th>2017</th>
<th>2018</th>
<th>2019</th>
<th>2020</th>
<th>2021</th>
</tr>
</thead>
<tbody>
<tr>
<th colspan="6">Cash flows provided (used) by operating</th>
</tr>
<tr>
<td>Premiums Collected -net of ceded premium</td>
<td>33,150,000</td>
<td>33,813,000</td>
<td>35,179,045 </td>
<td>35,882,626 </td>
<td>35,882,626 </td>
</tr>
<tr>
<td>Loss and LAE Paid</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
</div>
<div class="tab-pane fade" id="base-strategy--fs-balancesheet" role="tabpanel" aria-labelledby="table-tab" aria-expanded="false">
<table class="table">
<thead>
<tr>
<th>As of the end of the period</th>
<th>2017</th>
<th>2018</th>
<th>2019</th>
<th>2020</th>
<th>2021</th>
</tr>
</thead>
<tbody>
<tr>
<th colspan="6">Assets</th>
</tr>
<tr>
<td>Cash, Cash Equivalents </td>
<td>18,387,125</td>
<td>33,813,000</td>
<td>35,179,045 </td>
<td>35,882,626 </td>
<td>35,882,626 </td>
</tr>
<tr>
<td>Short Term Investments</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
答案 0 :(得分:0)
如果您想根据锚点击和切换显示div,请按以下步骤操作。
displayDivNumber:number;
onAnchorClick(no) {
this.displayDivNumber = no;
}
html with ngSwitch
<a (click)= "onAnchorClick(1)"> </a>
<a (click)= "onAnchorClick(2)"> </a>
<a (click)= "onAnchorClick(3)"> </a>
<div [ngSwitch]="displayDivNumber">
<div *ngSwitchCase="1">Tab content 1</div>
<div *ngSwitchCase="2">Tab content 2</div>
<div *ngSwitchCase="3">Tab content 3</div>
</div>
或者如果需要单独打开/关闭,那么您应该尝试通过为每个创建单独的变量来执行以下操作。
displayDiv1:boolean=true;
displayDiv2:boolean=false;
displayDiv3:boolean=false;
onAnchorClick(no) {
if(no===1) {
displayDiv1 = !displayDiv1;
} else if(no===2) {
displayDiv2 = !displayDiv2;
} else if(no===3) {
displayDiv3 = !displayDiv3;
}
}
html
<a (click)= "onAnchorClick(1)"> </a>
<a (click)= "onAnchorClick(2)"> </a>
<a (click)= "onAnchorClick(3)"> </a>
<div [ngSwitch]="displayDivNumber">
<div *ngIf="displayDiv1">Tab content 1</div>
<div *ngIf="displayDiv2">Tab content 2</div>
<div *ngIf="displayDiv3">Tab content 3</div>
</div>