简体   繁体   中英

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?

This is what i am trying now but the class does not change from unsel-source to sel-source

My source.component.html file looks like this:

<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:

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

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
  • 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
  • back in the html, dynamically set a class with ngClass depending on whether the index is the selected index

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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