How to change a css class in the parent component by clicking a button on the child component?

Asked

Viewed 313 times

0

I am new to developing projects with Angular. I need, please, a great help.

I would like a class css was changed in a <div></div> inserted into the parent component and when a button was clicked on the child component the classes "sucess" and "Danger" alternate (toggle).

Below is an example:

Father.component.html

<app-child></app-child>
<div [ngClass]="status ? 'success' : 'danger'"></div>

Father.component.ts

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

@Component({
  selector: 'app-father',
  templateUrl: './father.component.html',
  styleUrls: ['./father.component.scss'],
})
export class FatherComponent implements OnInit {

  status: boolean = false;

  constructor() { }

  ngOnInit(): void {
  }

}

Child.component.html

<button (click)="clickEvent()">Mudar classe css no componente pai</button>

Child.component.ts

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

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit {

  status: boolean = false;
    clickEvent(){
    this.status = !this.status;       
  }

  constructor() { }

  ngOnInit(): void {
  }

}

  • @Input() passes values from the parent component to child components, whereas the opposite is to pass values from the child component to the parent vc uses @Output() along with Eventemiter(), This is basic in Angular.

  • Yes this concept I know and I know it is basic, I was having trouble with the syntax for not having experience with the angular, but I already solved, I’ve made it work correctly.

  • 1

    Yeah, yeah, yeah man.

1 answer

-1


Follow my answer:

Father.component.html

<app-child (myEvent)="receiveEvent($event)"></app-child>
<div [ngClass]="status ? 'success' : 'danger'"></div>

Father.component.ts

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

@Component({
  selector: 'app-father',
  templateUrl: './father.component.html',
  styleUrls: ['./father.component.scss'],
})
export class FatherComponent implements OnInit {

  status: boolean = false;
  
  receiveEvent($event) {
    this.status = $event
  }

  constructor() { }

  ngOnInit(): void {
  }

}

Child.component.html

<button (click)="clickEvent()">Mudar classe css no componente pai</button>

Child.component.ts

import { Component, OnInit, Output, EventEmitter} from '@angular/core';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit {

  status: boolean = false;
  
  @Output() myEvent = new EventEmitter();
  
  clickEvent(){
    this.status = !this.status;
    this.myEvent.emit(this.status)
  }

  constructor() { }

  ngOnInit(): void {
  }

}

  • Should have a Father status output for Child, if not Father Component is dependent on Child for your template, beware of communication between Components..

  • This application is already ready, this is the best way not to have to make major changes to the components, the code above is a very small part of the application, so negativizing the answer makes no sense without knowing the dimension of the project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.