在angularjs的ng-click函数中获取元素的属性 [英] Getting attribute of element in ng-click function in angularjs

查看:34
本文介绍了在angularjs的ng-click函数中获取元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的 span 标签,当点击它时会调用控制器中的一个函数.

HTML

<div class="col-lg-1 text-center"><span class="glyphicon glyphicon-trash" data={{event.id}} ng-click="deleteEvent()"></span>

控制器

$scope.deleteEvent=function(){控制台日志(这个);}

我需要在控制器函数中获取数据属性中的值.我尝试使用这个关键字和 $event;一个都没有用.

请帮忙.

解决方案

尝试将其直接传递给 ng-click 函数:

<span class="glyphicon glyphicon-trash" data="{{event.id}}"ng-click="deleteEvent(event.id)"></span>

然后它应该在您的处理程序中可用:

$scope.deleteEvent=function(idPassedFromNgClick){console.log(idPassedFromNgClick);}

这是一个例子

I have a span tag like below which call a function in the controller when clicked.

HTML

<div class="row" ng-repeat="event in events">
    <div class="col-lg-1 text-center">
        <span class="glyphicon glyphicon-trash" data={{event.id}} ng-click="deleteEvent()">
        </span>
    </div>
</div>

Controller

$scope.deleteEvent=function(){
    console.log(this);
}

I need to get the value in data attribute in the controller function. I tried using this keyword and $event; neither one worked.

Please help.

解决方案

Try passing it directly to the ng-click function:

<div class="col-lg-1 text-center">
    <span class="glyphicon glyphicon-trash" data="{{event.id}}"
          ng-click="deleteEvent(event.id)"></span>
</div>

Then it should be available in your handler:

$scope.deleteEvent=function(idPassedFromNgClick){
    console.log(idPassedFromNgClick);
}

Here's an example

这篇关于在angularjs的ng-click函数中获取元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆