0

Trying to add ngClass to a div after a series of 5 button clicks.

Here is the button located on the app.component.html file:

<button class="btn btn-primary" (click)="onToggleDetails()">Display Details</button>

<p *ngIf="showSecret">Secret Password = tuna</p>

<div *ngFor="let logItem of log" 
  [ngStyle]="{backgroundColor: logItem >= 5 ? 'blue' : 'transparent'}"
  [ngClass]="{'white-text': logItem >= 5}"
>{{ logItem }}</div>

Here is the app.component.ts file:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'], *** EDIT ***
  styles: [`
  h3 {
    color: dodgerblue;
  }
  `]
})
export class AppComponent {
  username1 = '';
  showSecret = false;
  log = [];
  onToggleDetails(){
  this.showSecret = !this.showSecret;
  this.log.push(this.log.length + 1);
  }
}

Here is app.component.css

.white-text{
  color: white;
}

Currently, the logItem background remains blue after the 5th button click. I can inspect the console and see that the class .white-text has been added, but the text remains black.

Here is how it looks:

enter image description here

When I inspect the element, you can see the class has been added:

enter image description here

I am new to Angular and I am following a udemy course and got stuck here. I cannot proceed to the next part unless I fix this.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89

2 Answers2

1

Change your component definition as per below to include the CSS file.

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.cs']
  })
user3392782
  • 181
  • 4
0

log : any [ ] = [ ];

This was another solution when searching for a more specific question I will give credit where credit is due. What is "not assignable to parameter of type never" error in TypeScript?

  • Welcome to SO! Try to format it better, like `log : any [] = [];`. And it is better "visually" if the link encompasses less words, not all the phrase. – Marcelo Scofano Diniz Oct 25 '22 at 16:58
  • 1
    @MarceloScofanoDiniz thank you for your input and the welcoming. Any input to better oneself is always appreciated by those open to it. I appreciate it. – Joshua Rice Oct 29 '22 at 02:55