AngularJS ng-styleDirective

AngularJS 参考手册AngularJS Reference Manual


AngularJS Example

Add styles using AngularJS, using CSS key=>value object format:

<body ng-app="myApp" ng-controller="myCtrl">

<h1 ng-style="myObj">Example Tutorial</h1>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.myObj = {
        "color" : "white",
        "background-color" : "coral",
        "font-size" : "60px",
        "padding" : "50px"
    }
});
</script>
</body>

Try it Yourself »

Definition and Usage

ng-styleThe directive adds a style attribute to HTML elements.

ng-styleThe attribute value must be an object, and the expression returns an object as well.

The object consists of CSS properties and values, i.e., key=>value pairs.


Syntax

<element ng-style="expression"></element>

All HTML elements support this attribute.


Parameter Value

Value Description
string The expression returns an object composed of CSS properties and values.

AngularJS 参考手册AngularJS Reference Manual

Other Extensions