An expression which returns an object whose keys are CSS name and the values are CSS values.
Sample coding for ng-style directive in AngularJS:
Code Explanation for ng-style directive in AngularJS:
AngularJS is distributed as a JavaScript file, and can be added to a HTML page with a <script> tag.
The AngularJS application is defined by ng-app="myApp". The application runs inside the <body> tag. It’s also used to define a <body> tag as a root element.
The ng-controller=”styleCtrl“ is an AngularJS directive. It is used to define a controller name as “styleCtrl”.
The ng-style=”styleObj” is used to specify the CSS style attribute for the <h1> HTML element.
The angular.module function is used to create a Module.
Here we have declared a controller styleCtrl module using apps.controller() function. The value of the controller modules is stored in scope object. In AngularJS, $scope is passed as first argument to apps.controller during its constructor definition.
$scope.styleObj={ “color”: “white”, “background-color”: ”orange”, “font-size” : “40px”}. Here we have declared an CSS object as “styleObj” and set the CSS name and their value.
Sample Output for ng-style directive in AngularJS:
The output displays that <h1> content as “Welcome to AngularJS Tutorials”. And the content is displayed with font color as white, background color as orange and font size as 40 pixels by using the ng-style directive in <h1> element.