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
In Angular, a Directive is essentially a typescript class which has been annotated with a TypeScript Decorator. The decorator is the @ symbol.
Note: When you create a directive, it is restricted to attribute and elements only by default. In order to create directives that are triggered by class name, you need to use the restrict option. The restrict option is typically set to: 'A' - only matches attribute name.
Directives are classes that add new behavior or modify the existing behavior to the elements in the template. Basically directives are used to manipulate the DOM, for example adding/removing the element from DOM or changing the appearance of the DOM elements.
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
You can use the angular.element function. It's basically the same as a jquery element $(element)
Just do:
angular.element(element).addClass('myDraggable');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With