简体   繁体   English

如何使用angular.js更改元素的颜色

[英]How to change color of elements with angular.js

How can I change color of elements using css classes with angular js. 如何使用带角度js的css类更改元素的颜色。 I read some stuff online but somehow I am missing something as it does not work for me. 我在网上阅读了一些资料,但是由于某种原因我无法使用它,因此我想念一些东西。

My js fiddle: 我的js提琴:

http://jsfiddle.net/ynko3ct3/1/ http://jsfiddle.net/ynko3ct3/1/

Code: 码:

<p class="change-background-color">No difference!</p>
 <form>
 <div class="form-group">
      <label>Header color</label>
      <input class="form-control" type="text"  ng-model="Headercolor" placeholder="#f00">
    </div>
    </form>

css CSS

.change-background-color { background-color: {{Headercolor}}; }

Please check the updated fddle: http://jsfiddle.net/ynko3ct3/2/ 请检查更新的文件: http : //jsfiddle.net/ynko3ct3/2/

You should use ng-style directive to change style attribute in HTML, not in CSS like this: 您应该使用ng-style指令在HTML中更改样式属性, 而不是在CSS中更改样式属性,如下所示:

<p class="change-background-color" ng-style="{'background-color':Headercolor}">
  No difference!
</p>

you can check also this link : PLUNKER DEMO 您也可以检查此链接: PLUNKER DEMO

need to use ng-style="yourVariableName" in your <p></p> tag and call a function on change of input field to set input field color for <p></p> tag. 需要在<p></p>标记中使用ng-style="yourVariableName"并在输入字段更改时调用函数以为<p></p>标记设置输入字段颜色。

In html: 在html中:

<p ng-style="backColor">this is a simple background color seter</p>
<input ng-model="headerColor" ng-change="setColor()">

and in your controller: 并在您的控制器中:

$scope.headerColor = '#cd0a0a';
  $scope.backColor = {'background-color':$scope.headerColor};
  $scope.setColor = function() {
    $scope.backColor = {'background-color':$scope.headerColor};
  };

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

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