最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set css style dynamically with angularjs? - Stack Overflow

programmeradmin5浏览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.

<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 badges
Add a ment  | 

3 Answers 3

Reset to default 5

Try 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}">
发布评论

评论列表(0)

  1. 暂无评论