【问题标题】:How do I get the div ID in AngularJS如何在 AngularJS 中获取 div ID
【发布时间】:2014-03-31 14:15:15
【问题描述】:

我有一个使用 ng-repeat 的对象数组创建的 div。在该 div 中是一个很小的辅助 div,旨在充当删除。当我单击第二个 div 时,我希望将对象从数组中删除。

HTML:

<div ng-repeat="item in items" id = "doc:{{$index}}">
    <div ng-model="remove" ng-click="removefunc()" id="{{$index}}">
        x
    </div>
    Other stuff goes here...
</div>

AngularJS:

$scope.removefunc = function() {
    $scope.items.splice(ID of the div,1);
}

这是正确的方法吗?如果是这样,我如何获得 div 的 ID?

【问题讨论】:

  • 没关系....只要传递 $index 就可以了 ng-click="removefunc($index)

标签: arrays html angularjs object dom


【解决方案1】:

为您提供了一些修复(id 标签对于您正在执行的操作不是必需的):

将索引传递给函数,以便它知道是哪一个。

<div ng-repeat="item in items">
    <div ng-model="remove" ng-click="removefunc($index)">
        x
    </div>
    Other stuff goes here...
</div>

还有:

在拼接中使用该索引。

$scope.removefunc = function(index) {
    $scope.items.splice(index, 1);
}

请注意,如果您的中继器上有过滤器,那么这将不起作用

【讨论】:

  • 谢谢。我也意识到不需要该 id 并已将其删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-13
  • 2017-01-19
  • 2015-08-10
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
相关资源
最近更新 更多