Per definition:
Explanation:
The difference is in that style
, allows you to set the element's style directly via the attribute thats native to the element, but if you'd want to, for example, do something more dynamic, such as have the style change on a certain condition. You'd need to use JavaScript to do so. In the context of AngularJS, that is where ng-style
comes in.
ng-style
will allow you to alter or set the style of an element using Angular JavaScript based on whatever your need is at any specific time.
Example:
Here is a fiddle demonstrating the usage of both (or see below)
In your markup.
<div style="background: red"> this is set using the style property</div>
<div ng-style="getStyle('111111')"> set using ng-style</div>
In your angular controller.
getStyle = function(colour) {
return {"background": "#" + colour};
}
In the above example getStyle
is a function that allows you to pass in a colour value that the ng-style
directive in turn, will bind to the DOM element. Of course this can get as dynamic or flexible as per what you need want to do. (eg have the colour change on a click of a button, on a hover event, etc.)