テーブルの特定の要素(下の$scope
のコード)をクリックすると、その要素の情報を取得したいとします(例のコードでは「apple」)。AngularJSスコープを使用してonClickイベントを要素のテーブルに添付するにはどうすればよいですか?
現在は、私が$scope.studentsObj
内の要素を検索し、それらのすべてにonClick
を添付トラブルを抱えている(これは$scope.studentsObj.push
機能によって追加されたものが挙げられる。)
また、今私が使用している場合$scope.studentsObj.push
、それは常に次の行に行きます、どのように特定のキー値に要素をプッシュしますか?たとえば、行を追加する場合は、"first: apple, last: juice"
に置き換える必要があります。
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<body>
<div ng-app="myApp" ng-controller="myController">
<h1>Table of Names/h1>
<table>
<th> <h2>First Name</h2></th> <th> <h2>Last Name</h2></th>
<tr ng-repeat="student in studentsObj">
<td ng-repeat="(key, value) in student"> {{value}} </td>
</tr>
</table>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.studentsObj =
[ {first: "apple", last: "pie"},
{first: "dance", last: "marathon"},
{first: "tom", last: "boy"}],
$scope.studentsObj.push({first:"karen"}),
$scope.studentsObj.push({first:,last:"smith"});
});
</script>
</body>
</html>
することができます単に 'ng-click =" addStudent() 'のどこかに設定して、コントローラの' $ scope'オブジェクトに 'addStudent'関数を追加してください。 – devqon