简体   繁体   English

如何在一个元素上添加一个类并从所有同级元素中删除一个类

[英]how to add a class to one a element and remove a class from all siblings a elements angular

I have a simple menu bar across the top of my page. 我在页面顶部有一个简单的菜单栏。 I would like to click an element and have the inverse color/background-color. 我想单击一个元素并具有反色/背景色。 Obviously i need to be able to reset all my sibling elements back to the default color/background-color before changing the style on the clicked element. 显然,在更改被单击元素的样式之前,我需要能够将所有同级元素重置为默认颜色/背景色。

so far my click event function looks like this 到目前为止,我的点击事件功能看起来像这样

onSelect(event, source: Source): void {
    console.log(event.target);

    //reset all a elements do background-color:white and color:white
    var siblings = event.target.parentNode.children;
    for(var i=0; i<siblings.length; i++) {
      siblings[i].style.backgroundColor = "white";
      siblings[i].style.color = "black";
    }

    event.target.style.backgroundColor = "black";
    event.target.style.color = "white";
    this._sharedService.publishData(source.id);

  }

Is there a better way or a more "angularic" way or cleaner way to achieve this in Angular 2? 在Angular 2中,是否有更好的方法或更“角化”的方法或更清洁的方法来实现这一目标?

This is what i am trying now but the class does not change from unsel-source to sel-source 这就是我现在正在尝试的方法,但是该类不会从unsel-source更改为sel-source

My source.component.html file looks like this: 我的source.component.html文件看起来像这样:

<nav id="nav" class="fixed sources-main">
  <div class="flex flex-wrap pl1 border-bottom">
      <a *ngFor="let source of sources; let i = index"
      [ngClass]="{'sel-source':isSelected === i}" 
      (click)="onSelect(i, source)" 
      class="h5 bold mr1">
        {{source.name}}
      </a>
  </div>
</nav>

the source.component.ts file looks like this: source.component.ts文件如下所示:

import { Component, OnInit } from '@angular/core';
import {SourcesService} from './sources.service'
import { Source } from './source';

import { SharedService } from '../shared.service';

@Component({
  selector: 'app-sources',
  templateUrl: './sources.component.html',
  styleUrls: ['./sources.component.css'],
  providers:[SourcesService]
})
export class SourcesComponent implements OnInit {

  sources : Source[];
  isSelected;

  constructor(sourceService: SourcesService, private _sharedService: SharedService) { 
    sourceService.getSources().subscribe(sources => this.sources = sources);
  }

  ngOnInit() {
  }

  onSelect(index, source: Source): void {
    this.isSelected = index;
    this._sharedService.publishData(source.id);
  }

}

my styles.css file 我的styles.css文件

body, a {
    font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Open Sans,Helvetica Neue,Helvetica,sans-serif;
    line-height: 1.5;
    margin: 0;
    color: #111;
    background-color: #fff;
}

.sources-main {
    width:100%;
    background-color: #fff;
}

.articles-main {
    padding-top: 25px;
}

.sel-source {
    color:white;
    background-color: black;
}

You could try something like this: 您可以尝试这样的事情:

  • when you loop through the items in ngFor, also grab its index 当您遍历ngFor中的项目时,还要获取其索引
  • when the click event gets fired, also pass in the index in the loop and assign it to a variable 当点击事件被触发时,还要在循环中传递索引并将其分配给变量
  • have a property in the component that keeps track of the selected index (which will start by being undefined 在组件中具有一个属性,该属性可跟踪选定的索引(将从定义开始
  • in the click event handler, set the selected index to the value passed in 在click事件处理程序中,将所选索引设置为传入的值
  • back in the html, dynamically set a class with ngClass depending on whether the index is the selected index 回到html中,根据索引是否为所选索引动态设置ngClass类

import { Component } from '@angular/core';

@Component({
  selector: 'test',
  template: `
    <div
      *ngFor="let item of items; let i = index"
      [ngClass]="{'selected': selectedItem === i}"
      (click)="onItemClick(i)">
      {{ item }}
    </div>
  `,
  styles: [`
    .selected {
      background-color: black;
    }
  `]
})
export class AppComponent {
  selectedItem;

  items = ["A", "B", "C", "D", "E", "F", "G", "H"];
  onItemClick(index) {
    this.selectedItem = index;
  }
}

An angular way of doing it is to: 做到这一点的一种有角度的方法是:

  • make classes for your black/white color/background 为您的黑色/白色/背景制作类
  • use ng-class="" to get the correct class 使用ng-class=""获取正确的类

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

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