當我點擊表格上的特定元素(代碼爲$scope
)時,我想檢索該元素的信息(在示例代碼中稱爲「apple」)。如何將onClick事件附加到使用AngularJS作用域的元素表中?
目前,我無法在$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()'並在控制器 – devqon