AngularJS - 获取元素属性值 [英] AngularJS - get element attributes values

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

问题描述

你如何获得一个元素属性值?

How do you get an element attribute value?

例如。
HTML元素:

e.g. HTML element:

<button data-id="345" ng-click="doStuff($element.target)">Button</button>

JS:

function doStuff(item){
    angular.element(item)[0].data('id'); // undefined is not a function
}

任何建议多AP preciated,
的jsfiddle演示在这里: http://jsfiddle.net/h3TFy/

推荐答案

既然你要发送的目标元素到你的功能,你可以这样做,以获得ID:

Since you are sending the target element to your function, you could do this to get the id:

function doStuff(item){
    var id = item.attributes['data-id'].value; // 345
}

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

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