Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 
like image 681
Rishul Matta Avatar asked Dec 09 '13 13:12

Rishul Matta


People also ask

What is the decorator to declare a class as directive in Angular?

In Angular, a Directive is essentially a typescript class which has been annotated with a TypeScript Decorator. The decorator is the @ symbol.

What is restrict in AngularJS directive?

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.

What is directive class?

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.


2 Answers

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

like image 127
kfis Avatar answered Oct 09 '22 11:10

kfis


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

Just do:

angular.element(element).addClass('myDraggable'); 
like image 35
Graham T Avatar answered Oct 09 '22 12:10

Graham T