How to set div width using ng-style How to set div width using ng-style angularjs angularjs

How to set div width using ng-style


The syntax of ng-style is not quite that. It accepts a dictionary of keys (attribute names) and values (the value they should take, an empty string unsets them) rather than only a string. I think what you want is this:

<div ng-style="{ 'width' : width, 'background' : bgColor }"></div>

And then in your controller:

$scope.width = '900px';$scope.bgColor = 'red';

This preserves the separation of template and the controller: the controller holds the semantic values while the template maps them to the correct attribute name.


ngStyle accepts a map:

$scope.myStyle = {    "width" : "900px",    "background" : "red"};

Fiddle