如何使用angular.js中的jqlite
PHP中文网
PHP中文网 2017-05-15 16:50:05
0
6
586

文档中的说明:

angular.element(element)

我自己的调用:

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

根本没法执行,有大神给讲解一下吗?

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

PHP中文网
PHP中文网

认证0级讲师

reply all(6)
習慣沉默

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/

我想大声告诉你

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

淡淡烟草味

Change it to this:

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

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;
}
伊谢尔伦
angular.element(document).find('xx').addClass('yy')
仅有的幸福

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!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template