I want to show one div by default. If a button clicked, show the second div and if it clicked again, show the third div. I tried ng-if and ng-show and nothing happened.
<table class="">
<tbody>
<tr>
<td class="">Year</td>
<td class="">#1
<button class="button-primary" type="button" ng-model="cnt"
ng-click="cnt = cnt+1" ng-init="cnt=1">Add</button>
</td>
</tr>
<tr *ngFor="let year of yearList; index as i;">
<td class="">{{year}}</td>
<td class="" ng-show="cnt >= 1"><input class="" type="number"></td>
<td class="" ng-show="cnt >= 2"><input class="" type="number"></td>
<td class="" ng-show="cnt >= 3"><input class="" type="number"></td>
</tr>
</tbody>
</table>
The output should be:
Default:
year #1
1990 50
1995 10
after first click:
year #1 #2
1990 50 30
1995 10 40
after second click
year #1 #2 #3
1990 50 30 30
1995 10 40 30
Also, I don't know how to use function in ts and how to pass number of clicks.
<button class="" (click)="addScenario()">Add </button>
You should use
cnt >= 1. Ifcntbecomes 2, it hides the first input and shows the second input.You also need to remove the
=1from theng-model, because it doesn't allow assignment inside such a condition.Finally, I replaced the
trandtdwithdiv. It might have been because of the code snippet, but because we weren't in atableenvironment, thetrandtdwere removed - along with theng-showon thetd.You can also work with an array of values if you'd like, and extend that array on every click