Concept of AngularJS ng-style

Muhammad Adil Apr 21, 2022
  1. AngularJS ng-style Directive
  2. Steps to Use the ng-Style Directive in AngularJS
Concept of AngularJS ng-style

This article will explore the AngularJS ng-style with a conditional expression. The ng-style directive binds the style to an HTML element.

It is used when the style of an element needs to be updated dynamically.

AngularJS ng-style Directive

The ng-style can be used with a conditional expression to dynamically set the style of an HTML element based on some condition.

For example, if you have a button and you want it to have different colors depending on what color it is being hovered over, then you would use ng-style with a conditional expression and set the color of the button’s background to be dependent on whether or not it is being hovered over.

Syntax of ng-style:

<ANY ng-style="Any Conditional or non-conditional expression"></ANY>

The value of the ng-style directive can be any conditional binary or ternary expression. Furthermore, it receives any callback function defined within the console, which the controller can manage according to the needs.

We can also use the ng-style directive to manage Display and CSS styling based on the given value so that ng-style is invoked and the CSS styling is updated when the model value alters.

The ng-style directive is invoked at priority level 0 and has very few insertion limits. Because a few CSS styling names aren’t valid keywords for ng-style objects, it’s critical to always put the CSS styling key in single quotes.

Lastly, we can say that an ng-style directive is a powerful tool for developers looking to create a responsive design with dynamic content. It can create animations and transitions, apply different styles to different device sizes, etc.

Steps to Use the ng-Style Directive in AngularJS

The following steps are needed to use the ng-style directive in AngularJS.

  1. Add ng-style directive to HTML element.
  2. Add style content within the curly braces.
  3. Add style property with value for the given CSS property. The ng-style directive supports all CSS properties, including colors, fonts, and background colors.

Example of ng-style Directive in AngularJS

Let’s discuss an example to better understand AngularJS ng-style.

JavaScript Code:

var app = angular.module('ngStyleApp', []);
app.controller('ngStyle', function($scope) {
  $scope.bar = "88%";
});

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <style>
    .mainColor {
      background-color: pink;
    }
    .AvenColor {
      background-color: red;
    }
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
   <script src="app.js"></script>
</head>
<body ng-app="ngStyleApp" ng-controller="ngStyle">
  <div class="mainColor">
    <div class="AvenColor" ng-style="{'width':bar}">
      <h1>  {{bar}} Profile Complete</h1>
    </div>
  </div>
</body>
</html>

Click here to test the code’s live demonstration as mentioned above.

Muhammad Adil avatar Muhammad Adil avatar

Muhammad Adil is a seasoned programmer and writer who has experience in various fields. He has been programming for over 5 years and have always loved the thrill of solving complex problems. He has skilled in PHP, Python, C++, Java, JavaScript, Ruby on Rails, AngularJS, ReactJS, HTML5 and CSS3. He enjoys putting his experience and knowledge into words.

Facebook

Related Article - Angular Directive