带参数的ngClick不会传递参数

时间:2017-02-22 10:01:23

标签: javascript angularjs parameters angularjs-ng-repeat parameter-passing

我有一张带有两次重复重复的树状表。

<table>
<tr ng-repeat-start="am in anArray">
<td><button ng-click="TheFunction(am)"></button></td>
</tr>
<tr ng-repeat-start="em in anotherArray">
<td><button ng-click="TheFunction2(em)"></button></td>
</tr>
<tr ng-repeat-end></tr>
<tr ng-repeat-end></tr>

使用某些功能显示/隐藏&#34;正确&#34;行结果在屏幕上是一个树,每行都有一个按钮来调用范围中定义的函数。 在控制器中我定义了函数:

$scope.TheFunction=function(am){
alert(am); //and other things to do with the am object
}
$scope.TheFunction2=function(em){
alert(em); //and other things to do with the am object
}

如果我运行该页面,树状结构按预期工作,所有按钮都会显示。如果单击按钮,则调用正确的函数。但是,当我检查传递的参数时,它是未定义的。

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

以下是您的代码..它适用于ng-repeatng-repeat-start。我使用ng-repeat,因为它更干净..

看看这是否适合你:

&#13;
&#13;
var app = angular.module('app', []);
app.controller('main', main)

function main($scope){
  $scope.anArray = ["a", "b", "c"];
  $scope.anotherArray = ["dog", "cat", "mouse"];
  $scope.TheFunction=function(am){
    alert(am); //and other things to do with the am object
  }
  $scope.TheFunction2=function(em){
   alert(em); //and other things to do with the am object
  }

}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app ng-view ng-controller="main">
<table>
<tr ng-repeat="am in anArray">
  <td><button ng-click="TheFunction(am)">{{am}}</button></td>
</tr>
<tr ng-repeat="em in anotherArray">
  <td><button ng-click="TheFunction2(em)">{{em}}</button></td>
</tr>
</table>
</div>
&#13;
&#13;
&#13;