AngularJS - get element attributes values

后端 未结 8 1396
深忆病人
深忆病人 2020-12-04 12:24

How do you get an element attribute value?

e.g. HTML element:

         


        
8条回答
  •  臣服心动
    2020-12-04 12:40

    the .data() method is from jQuery. If you want to use this method you need to include the jQuery library and access the method like this:

    function doStuff(item) {
      var id = $(item).data('id');
    }
    

    I also updated your jsFiffle

    UPDATE

    with pure angularjs and the jqlite you can achieve the goal like this:

    function doStuff(item) {
      var id = angular.element(item).data('id');
    }
    

    You must not access the element with [] because then you get the pure DOM element without all the jQuery or jqlite extra methods.

提交回复
热议问题