简体   繁体   English

将类添加到AngularJs中的多个元素

[英]Add class to multiple elements in AngularJs

I am trying to add a class to multiple divs to change it's background color. 我试图将一个类添加到多个div来更改其背景颜色。

HTML: HTML:

<div class="span6">
    <div class="heading">Heading 1</div>                     
</div>

I am currently using this: 我目前正在使用此:

angular.element(document.querySelector('.heading')).addClass('color-change');

My issue is that... there are multiple div's with this heading but it is changing the color of only the first occurrence of that div. 我的问题是...这个标题有多个div,但它仅更改该div首次出现的颜色。 How can I have it so all the divs with that class add that css styling. 我怎么拥有它,所以所有具有该类的div都添加了CSS样式。

document.querySelector selects the first element that matches the query. document.querySelector选择与查询匹配的第一个元素。 querySelectorAll should achieve the result you want. querySelectorAll应该达到您想要的结果。

JSFiddle: https://jsfiddle.net/usw4cozt/ JSFiddle: https ://jsfiddle.net/usw4cozt/

Use ng-class instead of class and assign a className to your model : 使用ng-class代替class并为您的模型分配className:

<h1 ng-class='mainCtrl.selectedTheme'>Title 2</h1>

angular.module('demoApp', [])
  .controller('MainController', MainController)

function MainController() {
  this.selectedTheme = 'blue';
}

https://jsfiddle.net/4tuavhL2/ https://jsfiddle.net/4tuavhL2/

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

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