search

Home  >  Q&A  >  body text

How to use jqlite in angular.js

Instructions in the documentation:

angular.element(element)

My own call:

angular.element("xx").addClass("xx")

It’s impossible to implement it at all. Can anyone explain it?

http://jsfiddle.net/twn39/sy38y/1/

PHP中文网PHP中文网2776 days ago623

reply all(6)I'll reply

  • 習慣沉默

    習慣沉默2017-05-15 16:52:05

    Although angular.element is very close to jQuery, it is still not possible to obtain elements directly through HTML tags. The simplest way is as follows:

    angular.element(document.querySelector('p'));
    

    http://jsfiddle.net/sy38y/2/

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-15 16:52:05

    You can see it by looking at the document
    The parameter requirement of angular.element() is HTML string or DOMElement, but what you write is a selector, which does not meet the parameter requirement of angular.element. If you want to use this usage, you need to add jquery

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-15 16:52:05

    Change it to this:

        var par = document.getElementsByTagName('p')[0];
        angular.element(par).addClass('blue');
    

    reply
    0
  • 迷茫

    迷茫2017-05-15 16:52:05

    Angular does not recommend direct manipulation of the DOM, so if you want to add class, it is recommended to do so (I can't open your connection, I don't know how you wrote the code, so I will use my own)

    HTML

    <button ng-click="showActive()"></button>
    <p ng-class="{'active':show===1}"></p>
    

    js

    $scope.showActive=function(){
        $scope.show=1;
    }
    

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-15 16:52:05

    angular.element(document).find('xx').addClass('yy')

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-15 16:52:05

    Yes, I originally thought about passing the currently clicked button object through ng-click, but in the end I found that Angular does not allow this. . .
    I had no choice but to use angular’s ​​built-in jQlite to handle it!

    reply
    0
  • Cancelreply