简体   繁体   English

如何使用angularjs动态设置CSS样式?

[英]How to set css style dynamically with angularjs?

<div style="width: 50px !important">

I want to set the pixel number dynamically by angularjs. 我想通过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 ng-style="{width: (model.number * 30) +'px'}">

This works fine. 这很好。

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

这应该工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM