AngularJS supports .attr(), .removeAttr() jQuery functionality. Here in below examples we can see how to set attribute and remove attribute of an element.
Angular2 : If you are looking for Angular2 solution, please click here

Read or Assign value to attribute :

By using expressions, attribute values can be changed. Observe below code
....
<input ng-model="atribut"/>
....
<div myattr="{{atribut}}" id="divID">
  Attribute DIV <b>{{atribut}}</b>
</div>
<script>
function myCtrl($scope) {
    $scope.atribut="val 1";
}
</script>

Steps to read attribute value of an elemnt :

  1. Get angular element of html element
  2. Use .attr('attribute name') method to read the attribute value
var myEl = angular.element( document.querySelector( '#divID' ) );
alert(myEl.attr('myattr'));

Steps to set attribute value of an element :

  1. Get angular element
  2. Use .attr('attribute name', 'attribute value') method to set the attribute value
var myEl = angular.element( document.querySelector( '#divID' ) );
myEl.attr('myattr',"attr val");

Steps to remove attribute of an element :

  1. Get angular element
  2. Use .attr('attribute name', 'attribute value') method to set the attribute value
var myEl = angular.element( document.querySelector( '#divID' ) );
myEl.removeAttr('myattr');

20 comments:

  1. I need to set focus on a textbox when a hyperlink is clicked. I tried angular.element("#id").focus(), and $("#id").focus(), but nothing works. Is there an easier way to handle focus in angular?

    ReplyDelete
    Replies
    1. will that hyperlink redirects to new page or stays in same page ( with only # tag change) ?

      Delete
  2. does setting attribute work with angular version 1.5.0

    ReplyDelete
  3. what if the attribute is an object?

    ReplyDelete
  4. In the title it says "Set Attribute or Remove Attribute using AngularJS". Are you actually showing that?

    ReplyDelete
  5. do you have any idea on dynamic setting of attribute using angular JS,
    In jquery, there is prop method by which we can set attribute dynamicaaly

    ReplyDelete
  6. Exciting online games with us best casinos online come in and win right now.

    ReplyDelete
  7. Sehar News is a wide area that envelops Pakistan News, International News, Sports News, Arts and
    Entertainment News, Science and Technology, Business News, Education News and Today's Columns.
    The perusers can snatch most recent Urdu News dependent on different political and get-together
    occurring in the nation. Sehar News covers the most recent and up and coming news features, top
    stories from different backgrounds and carries it to the viewers. , Visit Daily http://www.seharnews.pk
    For latest updates and News,Thanks every one.

    ReplyDelete
  8. Ez battery reconditioning reviews - You can now easily revive your old batteries with this
    Ez battery reconditioning pdf which provides step by step instructions for recondition a battery.
    Ez battery reconditioning blog publishes how Ez battery reconditioning programs works
    and where buy Ez battery reconditioning step by step program online after this
    candid Ez battery reconditioning reviews. Battery reconditioning course is newbie friendly. It may help you
    set up and run your own battery reconditioning business by learning this skill at home.
    How to recondition a battery with Ez battery reconditioning .
    Have you heard about Tom Ericson's Ez battery reconditioning reviews technique and
    are wondering whether it is possible or not. visit https://ezbatteryreconditioninginfo.com/ this site to know more. Thank you every one.

    ReplyDelete
  9. I like you article. if you you want to saw Sufiyana Pyaar Mera Star Bharat Serials Full
    Sufiyana Pyaar Mera

    ReplyDelete
  10. Tech Gadgets reviews and latest Tech and Gadgets news updates, trends, explore the facts, research, and analysis covering the digital world.
    You will see Some Tech reviews below,

    lg bluetooth headset : You will also wish to keep design and assorted features in mind. The most essential part of the design here is the buttonsof lg bluetooth headset .

    Fastest Car in the World : is a lot more than the usual number. Nevertheless, non-enthusiasts and fans alike can’t resist the impulse to brag or estimate according to specifications. Fastest Car in the World click here to know more.

    samsung galaxy gear : Samsung will undoubtedly put a great deal of time and even more cash into courting developers It is looking for partners and will allow developers to try out
    different sensors and software. It is preparing two variants as they launched last year. samsung galaxy gear is very use full click to know more.

    samsung fridge : Samsung plans to supply family-oriented applications like health care programs and digital picture frames along with games It should stick with what they know and they
    do not know how to produce a quality refrigerator that is worth what we paid. samsung fridge is very usefull and nice product. clickcamera best for travel: Nikon D850: Camera It may be costly, but if you’re trying to find the very best camera you can purchase at this time, then Nikon’s gorgeous DX50 DSLR will
    probably mark each box. The packaging is in a vibrant 45.4-megapixel full-frame detector, the picture quality is simply wonderful. However, this is just half the story. Because of a complex 153-point AF system along with a brst rate of 9 frames per minute. camera best specification. click here to know more.

    visit https://techgadgets.expert/ this site to know more.

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts