首页 > 解决方案 > 使用 ng-style 更改输入占位符的颜色

问题描述

我的控制器中有 $scope 变量来定义我想嵌入到我的 html 代码中的输入占位符的颜色。

$scope 变量定义如下:

$scope.primaryColor={"color":colorVar}; //colorVar is the color in hex

color var 具有动态值,我想在输入 html 标记上使用 ng-style 来设置输入内占位符的颜色属性。

<input required name="email" ng-model="email" placeholder="hi@whatever.com" ng-style="">

我知道我无法通过内联 css 更改占位符颜色属性。有没有办法可以使用 ng-style 或任何其他方法将颜色变量传递给 css?

我怎样才能做到这一点?

标签: javascriptangularjs

解决方案


将 ng-class 添加到供应商前缀就可以了

HTML:

 <input type="text" placeholder="Quick test" required="" 
class="ng-pristine ng-invalid ng-invalid-required ng-touched"> 

文案:

.ng-touched::-webkit-input-placeholder { color: red; }
.ng-untouched::-webkit-input-placeholder { color: #cccccc; }

推荐阅读