如何在ng-repeat中使用ng-change

时间:2016-10-19 12:54:40

标签: javascript angularjs angularjs-scope

每次单击复选框时,我都会尝试在ng-change中执行一个函数。我需要获取模型的值或以某种方式知道单击了哪个复选框,以便我可以调用另一个相应的函数选中的值以及何时取消选中该复选框以便我可以输入一些值

不确定我使用的方法是否正确但这是我的代码: plunker

HTML

 <input type="checkbox" ng-model="data.model" ng-change="onChnage()" class='checkbox'>

JS

$scope.onChnage = function(){
    if($scope.index == true){
        $scope.indexonClick();
    } else if($scope.aggs == true){
        $scope.aggsonClick();
    } else if($scope.index == false){
        console.log('false');
    }
};

$scope.indexonClick = function(){
    alert('index Clicked!');
}

$scope.aggsonClick = function(){
    alert('aggs Clicked!');
};

4 个答案:

答案 0 :(得分:2)

重写您调用的函数,使其row作为参数:

... ng-change="onChange(data)" ...

...然后使用提供的行作为信息来源:

$scope.onChange = function(row){
  alert(row.name); // Aggregators or Index
};

Plunkr。作为旁注,我强烈建议您使用console.log代替alert

答案 1 :(得分:2)

请检查此代码,

// Code goes here

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  

  $scope.tableData = [{
    name: 'Index',
    url: '#',
    valueMain: 12,
    tValue: 13,
    model: 'index',
    subvalues: [{
      name: 'Keys',
      url: '#',
      valueSub: 544,
      tValue: 67
    }, {
      name: 'Leaders',
      url: '#',
      valueSub: 67,
      tValue: 89
    }]
  }, {
    name: 'Aggregators',
    url: '#',
    valueMain: 78,
    tValue: 91,
    model: 'aggs',
    subvalues: [{
      name: 'TPM',
      url: '#',
      valueSub: 11,
      tValue: 13
    }, {
      name: 'Pollster',
      url: '#',
      valueSub: 23,
      tValue: 45
    }]

  }];
  
  
  $scope.onChnage = function(value,test){
    console.log(test)
    if(value.model=='index'){
      $scope.indexonClick(test)
    } else if(value.model=='aggs'){
      $scope.aggsonClick(test)
    } else if($scope.index==false){
      console.log('false')
    }
  };
  
  $scope.indexonClick= function(test){
    var value = (test == true ? 'clicked' : 'un clicked')
    alert('index ' + value)
  }
  
  $scope.aggsonClick = function(test){
    var value = (test == true ? 'clicked' : 'un clicked')
     alert('aggs ' + value)
  };
});
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
  <script src="script.js"></script>
</head>

<body ng-controller="MainCtrl">
  <table class='table'>
    <thead>
      <tr>
         
        <th>name</th>
        <th>itemOne</th>
        <th>itemTwo</th>
        <th>model</th>
      </tr>
    </thead>
    <tbody ng-repeat="value in tableData| orderBy:'-valueMain'">
      <tr>
       
        <td>
          <button ng-show="value.expand" ng-click='value.expand = false'>-</button>
          <button ng-show="!value.expand" ng-click='value.expand = true'>+</button>
          <input type="checkbox" ng-model="test" ng-change="onChnage(value,test)" class='checkbox'
>
          <a rel="noopener" target="_blank" href={{value.url}}>
		{{value.name}}
		</a>
        </td>
        <td>{{value.valueMain}}</td>
        <td>{{value.tValue}}</td>
        <td>{{value.model}}</td>
        <tr>
          <tr ng-show="value.expand" ng-repeat="subs in value.subvalues| orderBy:'-valueSub'" >
            <td>
              {{subs.name}}
            </td>
            <td>
              {{subs.valueSub}}
            </td>
            <td>
              {{subs.tValue}}
            </td>
             
          </tr>
        </tr>

      </tr>
    </tbody>
  </table>
</body>

</html>

<强> Pls run this code snippet

Here is the plunker

答案 2 :(得分:1)

您需要做的是将值传递到onChnage()函数中。例如:

<input ... ng-change="onChnage(data.model)" ... />

请注意,您现在正在为onchange函数提供复选框的当前值

$scope.onChnage = function(isChecked){
    console.log(isChecked);

    ...
};

答案 3 :(得分:1)

将您的输入传递给您的功能

<input type="checkbox" ng-model="data.model" 
  ng-change="onChnage(data.model)" class='checkbox'>

部分解决了plunker:

https://plnkr.co/edit/aTo8FsMo5hgTGc2TM5Dx?p=preview