【问题标题】:Apply CSS style attribute dynamically in Angular JS在 Angular JS 中动态应用 CSS 样式属性
【发布时间】:2014-02-17 07:44:37
【问题描述】:

这应该是一个简单的问题,但我似乎找不到解决方案。

我有以下标记:

<div style="width:20px; height:20px; margin-top:10px; border:solid 1px black; background-color:#ff0000;"></div>

我需要将背景颜色绑定到范围,所以我尝试了这个:

<div style="{width:20px; height:20px; margin-top:10px; border:solid 1px black; background-color:{{data.backgroundCol}};}"></div>

那没有用,所以我做了一些研究,发现了ng-style,但是没有用,所以我试着去掉动态部分,只是在ng-style 中硬编码样式,就像这样。 ..

<div ng-style="{width:20px; height:20px; margin-top:10px; border:solid 1px black; background-color:#ff0000;}"></div>

这甚至行不通。我是否误解了ng-style 的工作原理?有没有办法将{{data.backgroundCol}} 放入普通样式属性并让它插入值?

【问题讨论】:

标签: javascript angularjs ng-style


【解决方案1】:

ngStyle 指令允许您在 HTML 元素上动态设置 CSS 样式。

Expression 评估一个对象,其键是 CSS 样式名称,值是这些 CSS 键的对应值。由于某些 CSS 样式名称不是对象的有效键,因此必须引用它们。

ng-style="{color: myColor}"

您的代码将是:

<div ng-style="{'width':'20px', 'height':'20px', 'margin-top':'10px', 'border':'solid 1px black', 'background-color':'#ff0000'}"></div>

如果你想使用作用域变量:

<div ng-style="{'background-color': data.backgroundCol}"></div>

Here 一个使用 ngStyle 的小提琴示例,下面是运行 sn-p 的代码:

angular.module('myApp', [])
.controller('MyCtrl', function($scope) {
  $scope.items = [{
      name: 'Misko',
      title: 'Angular creator'
    }, {
      name: 'Igor',
      title: 'Meetup master'
    }, {
      name: 'Vojta',
      title: 'All-around superhero'
    }

  ];
});
.pending-delete {
  background-color: pink
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller='MyCtrl' ng-style="{color: myColor}">

  <input type="text" ng-model="myColor" placeholder="enter a color name">

  <div ng-repeat="item in items" ng-class="{'pending-delete': item.checked}">
    name: {{item.name}}, {{item.title}}
    <input type="checkbox" ng-model="item.checked" />
    <span ng-show="item.checked"/><span>(will be deleted)</span>
  </div>
  <p>
    <div ng-hide="myColor== 'red'">I will hide if the color is set to 'red'.</div>
</div>

【讨论】:

  • 我想获得当前点击元素的原始背景颜色,而不是悬停时的背景颜色。使用以下给我悬停背景颜色,而不是原始颜色: $event.currentTarget.currentStyle.backgroundColor;知道如何获得原始背景颜色吗?
  • 应该是
【解决方案2】:

最简单的方法是调用样式的函数,并让函数返回正确的样式。

<div style="{{functionThatReturnsStyle()}}"></div>

在你的控制器中:

$scope.functionThatReturnsStyle = function() {
  var style1 = "width: 300px";
  var style2 = "width: 200px";
  if(condition1)
     return style1;
  if(condition2)
     return style2;
}

【讨论】:

  • 我不建议这样做,角度就是在视图中保留样式细节
  • 我也不推荐这个,它只适用于少数浏览器,如chrome,但如果你看IE 9+,这将不起作用
  • 其实这在IE11中还是不行,我从之前的项目中复制了一些代码,当它不起作用时很困惑,之前的项目使用的是Chrome
【解决方案3】:

直接来自ngStyledocs

表达式,它评估一个对象,其键是 CSS 样式名称和 values 是那些 CSS 键的对应值。

<div ng-style="{'width': '20px', 'height': '20px', ...}"></div>

所以你可以这样做:

<div ng-style="{'background-color': data.backgroundCol}"></div>

希望这会有所帮助!

【讨论】:

  • 是的,我在文档中看到了,但我看不到如何将其转换为多个规则,现在我可以看到它不是普通的 css 语法而是对象语法。
【解决方案4】:

一般来说,您可以结合使用 ng-if 和 ng-style 将条件更改与背景图像的更改结合起来。

<span ng-if="selectedItem==item.id"
      ng-style="{'background-image':'url(../images/'+'{{item.id}}'+'_active.png)',
                'background-size':'52px 57px',
                'padding-top':'70px',
                'background-repeat':'no-repeat',
                'background-position': 'center'}">
 </span>
 <span ng-if="selectedItem!=item.id"
       ng-style="{'background-image':'url(../images/'+'{{item.id}}'+'_deactivated.png)',
                'background-size':'52px 57px',
                'padding-top':'70px',
                'background-repeat':'no-repeat',
                'background-position': 'center'}">
 </span>

【讨论】:

    【解决方案5】:

    我会说您应该将不会更改的样式放入常规的style 属性,并将条件/范围样式放入ng-style 属性。此外,字符串键不是必需的。对于连字符分隔的 CSS 键,请使用驼峰式。

    <div ng-style="{backgroundColor: data.backgroundCol}" style="width:20px; height:20px; margin-top:10px; border:solid 1px black;"></div>
    

    【讨论】:

      【解决方案6】:

      只需这样做:

      &lt;div ng-style="{'background-color': '{{myColorVariable}}', height: '2rem'}"&gt;&lt;/div&gt;

      【讨论】:

        猜你喜欢
        • 2015-01-02
        • 2019-05-21
        • 2019-03-14
        • 1970-01-01
        • 2020-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-22
        相关资源
        最近更新 更多