comp5
comp5
@Injectable({
providedIn: 'root'
})
export class TransferService {
transferAmount: number = 0;
constructor() { }
getTransferAmount() {
return this.transferAmount;
}
setTransferAmount(amount: number) {
this.transferAmount = amount;
}
}
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class WithdrawService {
withdrawAmount: number = 0;
constructor() { }
getWithdrawAmount() {
return this.withdrawAmount;
}
setWithdrawAmount(amount: number) {
this.withdrawAmount = amount;
}
}
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
@Component({
selector: 'app-transfer',
template: `
<h2>Transfer</h2>
<p>Transfer amount: {{ transferService.getTransferAmount() }}</p>
<input type="number" [(ngModel)]="amount">
<button (click)="setTransferAmount()">Set transfer amount</button>
<button (click)="transfer()">Transfer</button>
`
})
export class TransferComponent implements OnInit {
amount: number = 0;
constructor(
public balanceService: BalanceService,
public transferService: TransferService,
public router: Router
) { }
ngOnInit() { }
setTransferAmount() {
// Set transfer amount
this.transferService.setTransferAmount(this.amount);
}
transfer() {
// Transfer funds
const transferAmount = this.transferService.getTransferAmount();
this.balanceService.transfer(transferAmount);
this.transferService.setTransferAmount(0);
this.router.navigateByUrl('/balance');
}
}