O
O
Oposum2020-11-14 09:49:44
Angular
Oposum, 2020-11-14 09:49:44

How to pass variable value from one component to another component?

Hello everyone and I apologize for a completely stupid question, but I can’t figure out such a task:
There are two components, one Home and the other Second.
The Home component renders the page, and the Second component "does something there" and changes the boolean in the console to the opposite value:

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

@Component({
  selector: 'app-second',
  templateUrl: './second.component.html',
  styleUrls: ['./second.component.scss']
})
export class SecondComponent implements OnInit {
other: boolean
  constructor() { }

  ngOnInit(): void {
this.other=true
console.log(this.other)
//что-то делает и потом
this.other=false
console.log(this.other)
  }

}

The crux of the issue I can't deal with is How do I pass the value of other to the Home component:
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
myBool: boolean
  constructor() { }

  ngOnInit(): void {
this.myBool= this.other // беру из second компонента?!
console.log(this.myBool)
  }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2020-11-14
@sHinE

There are different ways - https://angular.io/guide/component-interaction

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question