Comp
Comp
@Injectable({
providedIn: 'root'
})
export class AccountService {
balance = 5000;
withdraw(amount: number) {
if (amount <= this.balance) {
this.balance -= amount;
return true;
} else {
return false;
}
}
getBalance() {
return this.balance;
}
}
<h2>Balance</h2>
<a routerLink="/withdraw">Withdraw</a>
@Component({
selector: 'app-balance',
templateUrl: './balance.component.html',
styleUrls: ['./balance.component.css']
})
export class BalanceComponent implements OnInit {
balance: number;
ngOnInit() {
this.balance = this.accountService.getBalance();
}
}
<h2>Withdraw</h2>
<form (ngSubmit)="submitForm()">
<div class="form-group">
<label for="amount">Amount:</label>
<input type="number" id="amount" name="amount" [(ngModel)]="amount"
class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Withdraw</button>
</form>
@Component({
selector: 'app-withdraw',
templateUrl: './withdraw.component.html',
styleUrls: ['./withdraw.component.css']
})
export class WithdrawComponent {
amount: number;
submitForm() {
if (this.accountService.with