【问题标题】:document.queryselector of button in ng-repeatng-repeat 中按钮的 document.queryselector
【发布时间】:2014-03-31 16:49:10
【问题描述】:

如何在 ng-repeat 标记内的按钮上获取从 ng-click 函数内部单击的按钮?

我尝试这样设置 id:

<button class="btn btn-info ladda-button" type="button" 
    id={{'check_' + 'file.catModel}}
    ng-click="testButton('check_' + file.catModel)"
    data-style="expand-left">
    <i class="glyphicon glyphicon-check"></i><span class="ladda-label"> Check</span>
</button>

我尝试将 id 传递给函数,然后搜索:

angular.element
document.getElementById

但它们始终为空。

是否可以访问按钮?

【问题讨论】:

  • 可能有一种比直接访问 dom 更有角度的方式来做你想做的事。你想用这个按钮做什么?
  • 我正在尝试为每个单独的按钮设置一个 ladda 按钮加载轮 github.com/hakimel/Ladda

标签: javascript html angularjs button


【解决方案1】:

您可以将$event 属性传递给您的点击处理程序。

因此,您的代码将被重写为(为清楚起见省略其他属性):

<button ng-click="testButton('check_' + file.catModel, $event)">Check</button>

在您的点击处理程序中:

$scope.testButton = function (title, event) {
    //event parameter contains all details
    //of the button that invoked this handler
};

【讨论】:

  • 这就像我想要的那样工作,尽管由于某种原因,当我点击“检查”这个词时,该事件将 span 作为 toElement,所以我的函数没有不起作用,因为它需要一个按钮。你知道怎么解决吗?
  • 我刚刚删除了跨度,它看起来和我想要的一样并且可以正常工作。谢谢
猜你喜欢
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 2015-02-17
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多