I've created an Elevator app, with elevator component that displays a box with a value for the current floor, looking like this:
I've used a custom pipe to display the word 'lobby' instead of the number 1. I'm trying to add buttons to move elevator up and down by using a service (Elevator service -> up and down functions to decrease and increase the value in the box). Right now, my buttons are not working. I thought I need to use [(ngModel)] but I know I can't use it with a pipe. What am I doing wrong?
My code:
custom pipe-
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'lobbyPipe'
})
export class LobbyPipePipe implements PipeTransform {
transform(value: number): unknown {
if(value === 1){
return "lobby";
}
else {
return value;
}
}
}
service -
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ElevatorService {
floorNum: number=0;
constructor() { }
up(){
Number(this.floorNum);
this.floorNum++;
}
down(){
Number(this.floorNum);
this.floorNum--;
}
}
component-
export class ElevatorComponent implements OnInit {
@Input() floorNum : number =2;
dateFirst: Date=new Date();
constructor(private elevatorService: ElevatorService) { }
ngOnInit(): void {
}
up(){
this.elevatorService.up();
}
down(){
this.elevatorService.down();
}
}
html -
<div class="elevator">
floor:
<input class="input-warrper" [ngModel]="floorNum | lobbyPipe" name="floorNum" type="text">
<p *ngIf="floorNum===1">
{{dateFirst | date: "MM/dd/yy"}}
</p>
</div>
<input type="button" value="Go upstairs" (click)="up()">
<input type="button" value="Go downstairs" (click)="down()">