E
E
Eugene Zalivadnyi2015-10-22 21:23:41
Angular
Eugene Zalivadnyi, 2015-10-22 21:23:41

How to change the value of a field relative to the value of another field in Angular?

Good day. I need help with Angular. There is a project on Laravel. There are two fields. The first field is the number of participants. The second is the amount per participant.

<input type="number" ng-model="model.amount_users">
<input type="text" ng-model="model.price">

When creating and editing through this form, I have a task to divide the total amount by the number of participants and write this value into the second field. The total amount is displayed in the view via {{$game->price}}. Where to write the total amount and how to manipulate the second field?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nicholas, 2015-10-23
@agnamanshamansky

There are several options:
1. ng-change:

<input type="number" ng-model="model.amount_users" ng-change="onUserAmountChange()">
<input type="text" ng-model="model.price">

$scope.onUserAmountChange = function() {
    $scope.model.price = FULL_PRICE/$scope.model.amount_users;
}

2. markup:
<input type="number" ng-model="model.amount_users" ng-change="onUserAmountChange()">
<span> {{model.fullPrice/model.amount_users}} </span>

The second option is fine if the field is really just information for the user. Making it an input in this case makes no sense.

A
Andrew, 2015-10-22
@R0dger

hang $watch on the 1st variable and calculate what you need there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question