0
<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.

3 Answers 3

5

Try this:

<div ng-style="{width: (model.number * 30) +'px'}">
Sign up to request clarification or add additional context in comments.

1 Comment

If you need a couple of css rules simultaneously use commas: <div ng-style="{width: (model.number * 30) +'px', height: (model.number * 40) +'px',}">
2

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
  }
}]);

Comments

0

this should work

<div ng-style="{'width': model.number * 30}">

1 Comment

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. It's also recommended that you don't post an answer if it's just a guess. A good answer will have a plausible reason for why it could solve the OP's issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.