In simple terms:
Explanation:
The distinction lies in the fact that while style
allows direct styling of an element using its attribute, if you require dynamic changes like altering styles based on specific conditions, you would need to employ JavaScript. This is where ng-style
becomes useful in the realm of AngularJS.
ng-style
enables you to manipulate or define the style of an element using JavaScript as per your requirements at any given time.
Illustration:
Check out this demo showcasing the usage of both methods (or refer below)
In your code snippet.
<div style="background: red"> this is styled via the style property</div>
<div ng-style="getStyle('111111')"> styled with ng-style</div>
In your Angular controller.
getStyle = function(colour) {
return {"background": "#" + colour};
}
In the example above, the getStyle
function allows passing a color value which the ng-style
directive then binds to the DOM element. This can be further customized for various interactive functionalities like changing colors on button click or hover events.