<div style="width: 50px !important">
I want to set the pixel number dynamically by angularjs. The final width should also be calculated times a base pixel width. How could I achieve this?
<div ng-style="{{width: (model.number * 30)px !important}}">
This does of course not work, but shows what I'm trying to achieve. I'd like this do be done without having to introduce a controller function.
<div style="width: 50px !important">
I want to set the pixel number dynamically by angularjs. The final width should also be calculated times a base pixel width. How could I achieve this?
<div ng-style="{{width: (model.number * 30)px !important}}">
This does of course not work, but shows what I'm trying to achieve. I'd like this do be done without having to introduce a controller function.
Share Improve this question asked Oct 2, 2015 at 7:37 membersoundmembersound 87.1k218 gold badges675 silver badges1.2k bronze badges3 Answers
Reset to default 5Try this:
<div ng-style="{width: (model.number * 30) +'px'}">
This works fine.
HTML:
<div ng-app="myApp" ng-controller="myCtrl">
<p> Number: <input type="number" ng-model="model.number" /> </p>
<p> Times: <input type="number" ng-model="model.times" /></p>
<p style="outline:1px solid black; width:{{model.number*model.times}}px;" >Resulding width: {{model.number*model.times}} px</p>
</div>
Controller:
angular.module("myApp", []).controller("myCtrl", ["$scope", function($scope){
$scope.model = {
number: 50,
times: 2
}
}]);
this should work
<div ng-style="{'width': model.number * 30}">