1

我得到了一個有兩個ng重複的樹狀桌子。帶參數ngClick不通過參數

<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> 

對於某些功能來顯示/隱藏的「正確」線在屏幕上顯示的結果是用具有一個按鈕的每一行中調用的範圍限定的功能的樹。 在控制器I中定義的功能:

$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 
} 

如果我運行的頁面,樹狀構建體如預期工作,所有的按鈕被示出。如果我點擊一個按鈕,就會調用正確的函數。但是,當我檢查傳遞的參數時,它是未定義的。

有什麼我錯過了嗎?

+0

爲什麼不用NG重複和擺脫NG-重複端行的? –

+0

我使用開始/結束語法來使它更容易閱讀(至少對我來說),它不應該影響我認爲的參數傳遞? – Myko

+0

無法重現[JSF上的此DEMO](https://jsfiddle.net/m3wtdsnL/)的問題。 – georgeawg

回答

1

以下是您的代碼。它與ng-repeatng-repeat-start都有效。我用ng-repeat,因爲它是清潔..

看看這對你的作品:

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 
 
    } 
 

 
}
<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>

+0

除了未將應用程序和控制器定義複製到「我的」代碼中之外,您發生了什麼變化? – Myko

+0

使用了'ng-repeat'而不是'ng-repeat-start'和'ng-repeat-end' ..你可以看到這個簡單的例子在代碼片段中有效。如果你仍然無法使用它,請還張貼您的控制器,包括陣列的一部分.. –

+0

嗯,我仍然不明白爲什麼你的代碼工作,我的不是。 使用start-end-syntax時是否存在已知的錯誤? – Myko