There is the following task - to output an array of data using AngularJS. Part of the data is presented in numerical form and is placed in the input.

Accordingly, the value in this input can be edited and passed back to the API.

In code, the input looks like this:

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

The task is to ensure that the existing ng-model value, both during initialization and when data is changed, is not equal to that returned by the API, but, for example, divided by 10.

Is it possible to implement this?

I read many manuals and similar questions, but I could not find a solution. Thank you in advance!

UPDATE Not quite successfully put it in the question: The bottom line is that I want this type of construction

 <input type="number" ng-model="product.price / 10"> 

worked. That is, not only displayed the data, as is happening now, but also allowed to send this data.

UPDATE 2 The task is also complicated by the fact that I output the dataset using ng-repeat. That is, the final construction is as follows:

 <table> <tr ng-repeat="product in products"> <td>{{ product.name }}</td> <td><input type="number" ng-model="product.price"></td> </tr> </table> 

UPDATE 3 Also add an example of the controller code:

 directApp.controller('campaignController', function($scope, $http, $routeParams) { $http({ method: 'GET', url: 'url_to_get_data_from' }).success(function(data) { $scope.products = data; }); }); 
  • after getting api - go over all the data and change which you need - Grundy
  • I didn’t quite understand your answer, but I understood that I didn’t quite correctly formulate the question. Updated information. - kover-samolet
  • you get data in some place and fill in product.price why would you do product.price = value / 10 instead of product.price = value in the place of filling? - Grundy
  • in general it does not matter how you deduce: show how you fill in - Grundy
  • Again, my fault - I did not write that I deduce the data using ng-repeat. Thus, I do not see the possibility of displaying product.price modified inside the controller. - kover-samolet

1 answer 1

If they need additional processing before outputting the received data, it is best to do it right at the place of receipt once.
In this case, it is enough to go over them when receiving data and change the corresponding field, for example:

 directApp.controller('campaignController', function($scope, $http, $routeParams) { $http({ method: 'GET', url: 'url_to_get_data_from' }).success(function(data) { $scope.products = data.map(function(el){ el.price = el.price/10; //изменяем значение нужного поля return el; }); }); });