我正在学习angularjs,我能够列出我通过REST API收到的数据。我在一个表中显示数据,在其中一个列中我想要一个删除按钮,该按钮最终将进行DELETE调用以从数据库中删除该特定记录。这是我使用的HTML:
<table st-table="displayedCollection" st-safe-src="hosts" class="table table-striped">
<thead>
...
</thead>
<tbody>
<tr ng-repeat="x in displayedCollection">
<td>{{x.hostname}}</td>
<td>{{x.ipaddress}}</td>
<td>{{x.macaddress}}</td>
<td>
<button
type="button"
class="btn btn-default btn-sm"
ng-click="delete_record({{x._id}})">
<span class="glyphicon glyphicon-remove"></span>
</button>
</td>
</tr>
</tbody>
...
</table>
这给了我这个错误:
Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 16 of the expression [delete_record({{x._id}})] starting at [{x._id}})].
我的控制器看起来像这样:
app.controller("meanVoyCtrl", function($scope,$http) {
...
$scope.delete_record = function(id) {alert("id is ["+id+"]");};
...
});
现在,如果我只能alert
弹出显示要删除的记录的_id
,我会感到高兴。
我在这里做错了什么?
由于
答案 0 :(得分:1)
摆脱卷曲,只需:
ng-click="delete_record(x._id)">
答案 1 :(得分:1)
x._id周围不需要{{}}。应该是这样的:
array
0 =>
array (size=5)
'id' => string '1' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string 'info@test.de' (length=16)
1 =>
array (size=5)
'id' => string '3' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string 'kun@test.de' (length=26)
2 =>
array (size=5)
'id' => string '5' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string 'ni@test.de' (length=23)
3 =>
array (size=5)
'id' => string '410' (length=3)
'hostings_id' => string '5' (length=1)
'email' => string 'spam@email.de' (length=13)
4 =>
array (size=5)
'id' => string '148' (length=3)
'hostings_id' => string '5' (length=1)
'email' => string 'e@email.de' (length=23)
5 =>
array (size=5)
'id' => string '165' (length=3)
'hostings_id' => string '8' (length=1)
'email' => string 'mobil@newmail.de' (length=15)
6 =>
...
答案 2 :(得分:1)
如果你在一个角度指令之外,你只需要使用花括号(在这种情况下,&#39; ng-repeat&#39;)。它用作使用角度数据或控制器代码的方法。因为你已经在一个角度指令中,所以不需要花括号。您的按钮html将变为:
ng-click="delete_record(x._id)">