Reputation: 977
I'm pretty new to Angular and am having trouble incrementing and decrementing my variable using a button. I've looked around on here and tried what I've found and it doesn't seem to work. I'm trying to use the buttons, Next and Previous, to either add 1 or subtract 1 from my selectedEmployee variable and dynamically change what's output onto the screen.
Edit The Next function works as long as I don't select something from the combobox, it just adds 1 to the initialized 0 but doesn't work if I select something from the combobox. But, the Previous function works all of the time. Why would there be this difference? Also, the next function does work after I have used the previous function, but it just doesn't work the initial time after selecting an item from the combo box.
Here's my tracker.component.ts:
import { Component, OnInit, Input } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { EmpInfoService } from './emp-info.service';
import { EmpInfo } from './emp-info';
@Component({
selector: 'pto-tracker',
templateUrl: `./tracker.component.html`,
styleUrls: ['./tracker.component.css']
})
export class TrackerComponent implements OnInit{
empInfo: EmpInfo[];
isHidden: boolean = false;
public selectedEmployee: number = 0;
constructor(private empInfoService: EmpInfoService) { }
getEmpInfo(): void {
this.empInfoService.getEmpInfos().then(
empInfo => this.empInfo = empInfo
);
}
ngOnInit(): void {
this.getEmpInfo();
}
toggleSummary(): void {
this.isHidden = !this.isHidden;
}
nextEmployee(): void {
this.selectedEmployee = this.selectedEmployee + 1;
}
previousEmployee(): void {
this.selectedEmployee = this.selectedEmployee - 1;
}
}
and here's my tracker.component.html:
<div class="row">
<div [ngClass]="{'col-xs-12':isHidden === true, 'col-xs-7': isHidden !== false}" style="background-color:red;">
<button class="form-control" style="width:150px;" (click)="toggleSummary()">Open Summary</button>
<select id="empName" [(ngModel)]="selectedEmployee">
<option selected="selected" disabled>Employee Name...</option>
<option *ngFor="let emp of empInfo; let i = index" [value]="i">{{i}} - {{emp.EmpID}}</option>
</select>
<select id="PTOtype">
<option selected="selected" disabled>Type of PTO...</option>
<option value="PTO">PTO</option>
<option value="ETO-Earned">ETO - Earned</option>
<option value="ETO-Used">ETO - Used</option>
<option value="STDLTD">STD/LTD</option>
<option value="Uncharged">Uncharged</option>
</select>
<h2 *ngIf="empInfo && empInfo.length > selectedEmployee">{{empInfo[selectedEmployee].FirstName}}</h2>
<button class="form-control" style="width: 150px;" (click)="nextEmployee()">Next</button>
<button class="form-control" style="width: 150px;" (click)="previousEmployee()">Previous</button>
</div>
<div *ngIf="isHidden" class="col-xs-5">
<pto-summary [selectedEmployee]="selectedEmployee"></pto-summary>
</div>
</div>
Any help is greatly appreciated. Thanks in advance!
Upvotes: 2
Views: 271
Reputation: 625
I think problem in your select binding, you can use [ngValue]
instead [value]
in select.
<select id="empName" [(ngModel)]="selectedEmployee">
<option selected="selected" disabled>Employee Name...</option>
<option *ngFor="let emp of empInfo; let i = index" [ngValue]="i">{{i}} - {{emp.EmpID}}</option>
</select>
i in this case will be string, so calculate on it will be error.
Read this topic for more info:
Binding select element to object in Angular 2
Upvotes: 2