Search code examples
angularjsangularjs-ng-repeatangular-ngmodel

Change ng-modal input value with another input in-modal


I want to change value of an input which has its ng-model from another input ng-model value. It does not do so. ng-model="quantity" is in the ng-repeat, and ng-model="power" is outside of ng-repeat. So when I change value in power so the all quantity should change accordingly.

e.g

 <input type="text" ng-model="quantity" value="{{quantity * power}}">
 <input type="text" ng-model="power" value="{{power}}"> 

Solution

  • try this.

    angular.module('myApp', []).controller('mainCtrl', function ($scope) {
            $scope.quantities = [25, 6, 6.5, 80];
            $scope.quantitiesNewValue = [25, 6, 6.5, 80];
            $scope.power = 1;
     
       $scope.setValue = function(power){
          angular.forEach($scope.quantities, function(quantity,i){
             $scope.quantitiesNewValue[i] = power * quantity;
            });
         }
                     
        })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="mainCtrl">
    <h1>NG-MODEL Sample</h1>
    <p><input type="text" ng-model="power" ng-change="setValue(power)" value="{{power}}" /></p>
    <div ng-repeat="quantity in quantitiesNewValue">
    Quantity: <input type="text" ng-model="quantity" />
    </div>