获取Bootstrap中按钮的属性ID;AngularJS

Get the Attribute ID of Button in Bootstrap & AngularJS

本文关键字:ID AngularJS 属性 Bootstrap 按钮 获取      更新时间:2023-09-26

我在Bootstrap中创建了一个按钮,并为其分配了一个属性。单击按钮后,我希望获得函数中的属性值。然而,当我传入$element。在函数中获取未定义的值。请让我知道我的方法有什么问题。下面是代码

<div >
<a class="btn btn-info btn-circle text-uppercase" href="#" id="reply" ng-click="addReply($element.currentTarget)" data-id = "{{ comment.comment }}"><span class="glyphicon glyphicon-share-alt"></span> Reply</a>
<a class="btn btn-warning btn-circle text-uppercase" data-toggle="collapse" href="#replyOne" aria-expanded="true"><span class="glyphicon glyphicon-comment"></span> 2 comments</a>
</div>
$scope.addReply = function(item) {  
    var id = angular.element(item).data('id');
    alert(id);      
};

你应该使用angular指令。

myApp.directive('btnAttrClick', function() {
  return {
    link: function(scope, element, attr) {
      element.on('click', function() {
        alert(attr.dataId);
      })
    }
  }
})

和你的按钮:

<button btn-attr-click data-id="{{ comment.comment }}">Reply</button>

我没有测试代码,但你明白了要点——指令是让angular变得很棒的东西——学习它们,爱上它们

试试这个

$scope.addReply = function doStuff(item){
    var id = item.attributes['data-id'].value;
    alert(id);
}