最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the Id of the element in Angular js - Stack Overflow

programmeradmin6浏览0评论

I have assigned the id value of an element in angularjs to a variable like below

var test = angular.element("#sqldiv");

I'm sending the test variable to a function and wanted to use the id of the element in the function. I'm unable to retrieve the id value. when I try to put an alert for test. it just give [object,Object] as the output.Please let me know how to retrieve the id value of the element. i.e I wanted the value as "#sqldiv".

Any help on this is much appreciated. Thanks in Advance.

I have assigned the id value of an element in angularjs to a variable like below

var test = angular.element("#sqldiv");

I'm sending the test variable to a function and wanted to use the id of the element in the function. I'm unable to retrieve the id value. when I try to put an alert for test. it just give [object,Object] as the output.Please let me know how to retrieve the id value of the element. i.e I wanted the value as "#sqldiv".

Any help on this is much appreciated. Thanks in Advance.

Share Improve this question edited Feb 10, 2017 at 10:22 Raviteja 3,48923 gold badges45 silver badges74 bronze badges asked Feb 10, 2017 at 10:18 vr3w3c9vr3w3c9 1,1788 gold badges32 silver badges64 bronze badges 2
  • test[0].id, but please explain your usecase. Looks like you're doing stuff the nonangular-way – devqon Commented Feb 10, 2017 at 10:20
  • 2 This looks like you are trying to do something that could be done a lot cleaner - handling elements directly is almost always the wrong thing to do in Angular. Use data binding instead. – LionC Commented Feb 10, 2017 at 10:22
Add a ment  | 

2 Answers 2

Reset to default 2

Angular's jQlite works like jQuery with a little limitation of features to esure a good performance so that you can use jQuery's method $.fn.attr to do such thing.

For example:

var elm = angular.element(document.getElementById("#sqldiv"));

console.log(elm.attr('id'));

Note on angular.element: Its selector are limited so you can't query by id like that, you have to pass the element it self to jQlite like: angular.element(document.getElementById("#sqldiv")) or angular.element(document.querySelector("#sqldiv"))

Note on using DOM with angular: As it's been told on ments, avoid using DOM for data binding, angularjs has its own way to do that, most of activities doesn't require DOM manipulation, you should create a directive to do such thing, where you have the appropriated way to handle it.

try this

test[0].id

This will give you id of element.

发布评论

评论列表(0)

  1. 暂无评论