2017-09-19 73 views
-2

我的任務是顯示使用ng-repeat一個表,說了如:對每一行按鍵1234567。點擊按鈕後,我想顯示相應的號碼的警報。不同的警報點擊

以下是我的HTML文件。

<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> 
<script src="myApp.js"></script> 

<body ng-app="myApp" ng-controller="myCtrl"> 

    <table> 
    <tr ng-repeat="x in records"> 

     <td>{{x.Digit}}</td> 

     <td><input type="button" value="Press Me" ng-click="test(record)" /></td> 
    </tr> 
</table>    

</body> 

</html> 
<html> 

<body> 

JS文件。

var app = angular.module("myApp", []); 
app.controller('myCtrl', function($scope) { 
    // $scope.records = [1,2,3,4,5,6,7]; 
    $scope.records = [ 
    { 
     "Digit" : "1", 
    }, 

    { 
     "Digit" : "2", 
    }, 
    { 
     "Digit" : "3", 
    }, 
    { 
     "Digit" : "4", 
    }, 
    { 
     "Digit" : "5", 
    }, 
    { 
     "Digit" : "6", 
    }, 
    { 
     "Digit" : "7", 
    } 
    ] 
    $scope.test = function(text) { 
    alert("You clicked"); 
    // alert(text); 
    } 
// }).directive('li',function(){ 
// return { 
//  template: '<records> <record ng-click="test(record)" ng-repeat="record in records"> {{ record }} </record></br></records> ' 
// }  
// }); 

// app.controller("myCtrl", function($scope) { 

// $scope.myFunction = function(text){ 
//  alert(text); 
//  }; 
}); 
+0

你爲什麼取消標記答案? – Sajeetharan

回答

1

你需要傳遞x沒有record

<tr ng-repeat="x in records"> 
     <td>{{x.Digit}}</td> 
     <td><input type="button" value="Press Me" ng-click="test(x.Digit)" /></td> 
</tr> 

DEMO

var app = angular.module("myApp", []); 
 
app.controller('myCtrl', function($scope) { 
 
    // $scope.records = [1,2,3,4,5,6,7]; 
 
    $scope.records = [ 
 
    { 
 
     "Digit" : "1", 
 
    }, 
 

 
    { 
 
     "Digit" : "2", 
 
    }, 
 
    { 
 
     "Digit" : "3", 
 
    }, 
 
    { 
 
     "Digit" : "4", 
 
    }, 
 
    { 
 
     "Digit" : "5", 
 
    }, 
 
    { 
 
     "Digit" : "6", 
 
    }, 
 
    { 
 
     "Digit" : "7", 
 
    } 
 
    ] 
 
    $scope.test = function(text) { 
 
    alert("You clicked "+ text); 
 
    } 
 

 
});
<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> 
 
<body ng-app="myApp" ng-controller="myCtrl"> 
 
    <table> 
 
    <tr ng-repeat="x in records"> 
 
     <td>{{x.Digit}}</td> 
 
     <td><input type="button" value="Press Me" ng-click="test(x.Digit)" /></td> 
 
    </tr> 
 
</table>  
 

 
</body> 
 

 
</html> 
 
<html> 
 

 
<body>

1

由於您的迭代器是x,所以通過x.digit,沒有記錄。

<tr ng-repeat="x in records"> 
     <td>{{x.Digit}}</td> 
     <td><input type="button" value="Press Me" ng-click="test(x.Digit)" /></td> 
</tr>