$scope.delete = function(){
Here I want to get the t.number
};
<li class="item" ng-repeat="t in items">
<div class="row">
<div class="col col-10"><i></i></div>
<div class="col col-70">
<span class="number">{{t.number}}</span>
<p class="date">{{t.date}}</p>
</div>
<div class="col col-20">
<i class="ion ion-ios-paper" ng-click="Details()"></i>
<i class="ion ion-trash-a" ng-click="delete()"></i>
</div>
</div>
</li>
请指导我如何从控制器访问角度js中的html元素。
答案 0 :(得分:1)
将实体从html传递给控制器
试试这个
<强> HTML 强>
<i class="ion ion-trash-a" ng-click="delete(t)"></i>
<强> CTRL 强>
$scope.delete = function(entity){
console.log(entity.number);
// if you wanna delete item
var index = $scope.items.indexOf(entity);
$scope.items.splice(index,1);
};
答案 1 :(得分:1)
以下是您需要在ng-click="delete(items, $index)'
<li class="item" ng-repeat="t in items">
<div class="row">
<div class="col col-10"><i></i></div>
<div class="col col-70">
<span class="number">{{t.number}}</span>
<p class="date">{{t.date}}</p>
</div>
<div class="col col-20">
<i class="ion ion-ios-paper" ng-click="Details()"></i>
<i class="ion ion-trash-a" ng-click="delete(items, $index)"></i>
</div>
</div>
</li>
然后,在你的控制器中:
$scope.delete = function(array, index){
console.log('Item number: ',index); // Get element number
array.splice(index, 1); // Delete element
};
要解释一下,在您的ng-click
中,您发送的数组名称为items
,而index
(数组中的位置),则为delete
} function获取要删除的数组以及数组中项目的位置