Reputation: 649
I have 3 sections coming from loop. Also I have 3 buttons. Here I need to show only first section('one') by default and first button should be active/disabled.When I click second button second section should show only and second button should be active/disabled..so on.Till now working fine.But I have a 'down button' also,on click of that, click2 button will be trigger,again on click of 'down button' click3 button will be trigger.Here is code below
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div *ngFor="let data of testJson">
<div *ngIf="data.id==button_selected">
{{data.name}}
</div>
</div>
<div style="margin-top:10px">
<button *ngFor="let data of testJson"
style="display:block"
(click)="button_selected=data.id"
[disabled]="button_selected==data.id"
>
click {{data.id}}
</button>
</div><br>
<div><button>Down arrow</button></div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
button_selected = 1;
testJson = [
{
id: 1,
name: 'one'
},
{
id: 2,
name: 'two'
},
{
id: 3,
name: 'three'
}
];
name = 'Angular';
ngOnInit() {}
}
Upvotes: 0
Views: 698
Reputation: 4182
Add this method in your app.component.ts
.
down() {
if (this.button_selected < this.testJson.length) {
this.button_selected++;
}
}
and modify your down arrow button as below.
<div><button (click)="down()">Down arrow</button></div>
This is stackblitz sample.
Upvotes: 1
Reputation: 26390
Simply like this
<button (click)="button_selected++">Down arrow</button>
This will activate the next section every time this button is clicked.
Upvotes: 0