Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manipulate styles of directive in AngularJS?

I'm writing a component using AngularJS and AngularJS directives.

I'm doing something like this:

var MyApp = angular.module('MyApp', []);  MyApp.directive('myTag', function() {     return { /* Some logic here*/ } }); 

I want to be able to change style of my component (using CSS), something like this:

<my-tag class="MyClass"></my-tag> 

Besides this I want to be able to manipulate all elements style inside my component (HTML markup inside of my-tag).

Do you have any advice or useful examples how to manipulate the style properties of custom tags using AngularJS?

like image 671
Roman Dryndik Avatar asked Oct 08 '13 11:10

Roman Dryndik


People also ask

What is custom directive in AngularJS?

Custom directives are used in AngularJS to extend the functionality of HTML. Custom directives are defined using "directive" function. A custom directive simply replaces the element for which it is activated.

What is DOM manipulation in AngularJS?

When it comes to do DOM manipulation, binding event, etc... It happens, that we define functions that manipulates the DOM in a custom directive's link function, but we call it from the controller (we define functions in the $scope so it can be accessible by the given controller).

Can we create custom directive in AngularJS True False?

AngularJS comes with a set of these directives built-in, like ngBind , ngModel , and ngClass . Much like you create controllers and services, you can create your own directives for AngularJS to use.


1 Answers

This should do the trick.

var MyApp = angular.module('MyApp', []);  MyApp.directive('myTag', function() {     return {        link: function(scope, element, attributes){         element.addClass('MyClass');       }     } }); 
like image 112
Ben Avatar answered Sep 22 '22 11:09

Ben