0

我有一些低值和高值循环的 ng-repeat。

    1. LowValue1     HighValue1
    2. LowValue2     HighValue2
    3. LowValue3     HighValue3
    . . .
    n. LowValue n     HighValue n 

在 ng-repeat 中是这样的:

  <div ng-repeat="categoryObject in category.tiers track by $index"> 
         <div class="col-lg-3 ">
              <label class="control-label" translate> From ($) </label>
              <input type="number" class="form-control" ng-model="categoryObject.lowValue" >
         </div>
         <div class="col-lg-3 ">
              <label class="control-label" translate> To ($) </label>
              <input type="number" class="form-control" ng-model="categoryObject.highValue" >
         </div>
    </div>

我的要求是 1st highValue 应该等于 2nd lowValue 和 2nd highValue 应该等于 3rd lowValue 和儿子..

如何绑定这两个数据?

4

3 回答 3

1

像这样的东西?第一个 lowValue 和最后一个 highValue 没有绑定。但其余的都是相互依存的。

http://jsfiddle.net/xc7czgfz/

var myApp = angular.module('myApp',[]).controller("MyCtrl",["$scope",function($scope) {
    $scope.objs = [{lowValue:0,highValue:1},{lowValue:1,highValue:5},{lowValue:5,highValue:6}];
    $scope.handleChange = function(){
        console.log("handlechange");
        var index = arguments[0];
        var type = arguments[1];
        if(index!=null && type!=null){
            if(type == "low" &&(index > 0 && index< $scope.objs.length)){
                   $scope.objs[index-1].highValue= $scope.objs[index].lowValue;
            }
            if(type == "high" &&(index >= 0 && index< ( $scope.objs.length-1))){
                    $scope.objs[index+1].lowValue= $scope.objs[index].highValue;
            }
        
        }
        
    }	
      
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="MyCtrl">
   
<div ng-repeat="obj in objs"> 
     <div class="col-lg-3 ">
          <label class="control-label" translate> From ($) </label>
         
          <input type="number" class="form-control" ng-model="obj.lowValue" ng-change='handleChange($index,"low")'>
     </div>
     <div class="col-lg-3 ">
         <label class="control-label" translate> To ($) </label>
          <input type="number" class="form-control" ng-model="obj.highValue" ng-change='handleChange($index,"high")'>
     </div>
</div>
    </div>
  </div>

于 2015-10-14T05:59:19.013 回答
1

听起来您需要为第 n 个高值和第 (n+1) 个低值设置 1 个变量。

所以这可能是你的模型:

$scope.values = [firstLowValue, secondLowValues ...];
$scope.lastHighValue = lastHighValue;

并在您的模板中:

<div ng-repeat="value in values"> 
         <div class="col-lg-3 ">
              <label class="control-label" translate> From ($) </label>
              <input type="number" class="form-control" ng-model="values[$index]" >
         </div>
         <div class="col-lg-3 " ng-if="!$last">
              <label class="control-label" translate> To ($) </label>
              <input type="number" class="form-control" ng-model="values[$index + 1]" >
         </div>
<div class="col-lg-3 " ng-if="$last">
              <label class="control-label" translate> To ($) </label>
              <input type="number" class="form-control" ng-model="lastHighValue" >
         </div>
    </div>
于 2015-10-14T06:06:13.693 回答
1

您可以使用 ngChange 来完成此操作,请尝试:

<div ng-repeat="categoryObject in category.tiers track by $index"> 
         <div class="col-lg-3 ">
              <label class="control-label" translate> From ($) </label>
              <input type="number" class="form-control" ng-model="categoryObject.lowValue" >
         </div>
         <div class="col-lg-3 " ng-if="!$last">
              <label class="control-label" translate> To ($) </label>
              <input type="number" class="form-control" ng-model="categoryObject.highValue" ng-change="categoryObject.lowValue[$index+1]=categoryObject.highValue[$index]">
         </div>
          <div class="col-lg-3 " ng-if="$last">
              <label class="control-label" translate> To ($) </label>
              <input type="number" class="form-control" ng-model="categoryObject.highValue">
         </div>
    </div>
于 2015-10-14T06:17:55.143 回答