using element.AddClass to add an angular js directive, which is restricted to act as a 'class'

Rishul Matta picture Rishul Matta · Dec 9, 2013 · Viewed 54.6k times · Source

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

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

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

Answer

kfis picture kfis · Dec 14, 2013

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.

Something like

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

regards