使用element.AddClass添加一个角度js指令,该指令仅限于充当“类”

时间:2022-09-10 12:09:40

I made a directive in angular js restricted it to act as a class and this directive makes my element drag-able.

我在角度js中制定了一个指令,限制它作为一个类,这个指令使我的元素可以拖动。

On mousehover i want to do addClass and add this directive which is supposedly a class, but this doesnt give the desired result, am i missing something? below is what I am doing

在mousehover上我想做addClass并添加这个应该是一个类的指令,但这并没有给出想要的结果,我错过了什么?以下是我正在做的事情

module.directive('myDraggable', function ($document) {

   var directiveObject= {

   restrict:'AC',

//logic of dragging
}
return directiveObject;
});

i try to do

我试着去做

 element.addClass('myDraggable'); // but this fails! pls help

2 个解决方案

#1


44  

I guess, Angular is not observing elements, if they get a new directive attached at runtime. To make it work, you should recompile the element after adding the class with $compile.

我想,如果Angular在运行时附加了一个新的指令,它就不会观察元素。为了使它工作,你应该在使用$ compile添加类之后重新编译元素。

Something like

就像是

element.addClass('myDraggable');
$compile(element)(scope);

regards

问候

#2


8  

You can use the angular.element function. It's basically the same as a jquery element $(element)

您可以使用angular.element函数。它与jquery元素$(元素)基本相同

Just do:

做就是了:

angular.element(element).addClass('myDraggable');

#1


44  

I guess, Angular is not observing elements, if they get a new directive attached at runtime. To make it work, you should recompile the element after adding the class with $compile.

我想,如果Angular在运行时附加了一个新的指令,它就不会观察元素。为了使它工作,你应该在使用$ compile添加类之后重新编译元素。

Something like

就像是

element.addClass('myDraggable');
$compile(element)(scope);

regards

问候

#2


8  

You can use the angular.element function. It's basically the same as a jquery element $(element)

您可以使用angular.element函数。它与jquery元素$(元素)基本相同

Just do:

做就是了:

angular.element(element).addClass('myDraggable');