【问题标题】:Change ng-modal input value with another input in-modal用另一个模态输入更改 ng-modal 输入值
【发布时间】:2016-03-07 04:25:12
【问题描述】:

我想从另一个输入 ng-model 值更改具有 ng-model 的输入的值。它不这样做。 ng-model="quantity"ng-repeat 中,ng-model="power"ng-repeat 之外。因此,当我更改 power 中的值时,所有 quantity 都应相应更改。

例如

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

【问题讨论】:

  • 你能包含你所有的代码吗? ng-repeat 以及控制器将是理想的...根据您展示的内容,我无法完全确定您要做什么。
  • 请提供您的 ng-repeat 代码。
  • 下面是演示代码

标签: angularjs angularjs-ng-repeat angular-ngmodel


【解决方案1】:

试试这个。

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>

【讨论】:

  • 我试过这个,但遇到了另一个问题,它永久设置原始数组的值,所以当我想乘以另一个数字时,它会进一步乘以而不是重新乘以原始数组。
  • 你能有重复的数组吗?
【解决方案2】:

你在 html 中有两个错误。

1) 在power 的第一个输入字段中,您应该在 ng-model 中赋予了权力,在那里您还有错字,ng-mode 将是 ng-model

ng-model="power" 而不是ng-mode="p在此处输入代码ower"

2) 在 ng-repeat 块中,ng-model 应该是表达式 (quantity*power) 而不仅仅是数量

ng-model="quantity*power" 而不是ng-model="quantity"

Live Demo

您的 html 将是

<h1>NG-MODEL Sample</h1>
<p><input type="text" ng-model="power" value="{{power}}" /></p>
<div ng-repeat="quantity in quantities">
     Quantity: <input type="text" ng-model="quantity*power" value="{{quantity *         power}}"/>
</div>

【讨论】:

  • 感谢它完美地满足了我的需求。是的,我在发布问题时犯了这些错误。但是我转发了我的想法,你很好地解决了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多