So I’ve been playing around with angular and I learned about two-way binding using “ngModel”. I got this idea, but I can’t seem to implement it, would appreciate any help on this:
What I’m trying to do: So I will have an input field, that’ll take ticket prices for “Adult”, and a similar one for “Child”. I have another two input fields that’ll calculate the ticket prices based on the fixed rates. Then I want a third field where the sum of both their calculated prices is printed. I’ve tried this so far, but can't figure out what to do now. Any help would be greatly appreciated, thank you!
here’s the code:
<input type="text" [(ngModel)]="ticket.adult" name="adult">
<input type="text" value="{{ ticket.adult*15 }}">
<input type="text" [(ngModel)]="ticket.child" name="child">
<input type="text" value="{{ ticket.child*5 }}">
<p>Ticket Price:<!--the total price will be displayed here--></p>
P.S- I've declared a ticket object in its component.ts file.
{{ ticket.child*5 + ticket.adult*15 }}is all you need. And if you don't like these multiplications being duplicated, then add methods in your component (or in the Ticket class), and call them in the view.