【问题标题】:ng-style and custom css propertiesng 样式和自定义 css 属性
【发布时间】:2016-12-14 14:09:28
【问题描述】:
是否可以在 ng-style 中添加自定义 css 属性,即 css 变量?
我试过用这个:
<li ng-repeat="item in vm.items" ng-style="{'--index': $index}">{{item.name}}</li>
但这不起作用我在开发者工具中检查时没有 --index in style 属性。
【问题讨论】:
标签:
javascript
css
angularjs
css-variables
【解决方案1】:
不,您必须输入有效的 CSS 属性,例如
<li ng-repeat="item in vm.items" ng-style="{'z-index': $index}">{{item.name}}</li>
【解决方案2】:
试试这个方法:
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-style="myObj">Welcome</h1>
<script>
//u can put that <script> content in a separadet file
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.myObj = {
"color" : "white",
"background-color" : "coral",
"font-size" : "60px",
"padding" : "50px"
}
});
</script>
</body>