Answer the question
In order to leave comments, you need to log in
How to write and read property of parent component from child?
Hello.
I have a parent component in Angular:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<router-outlet></router-outlet>`
})
export class AppComponent {
auth: any = false;
}
import { Component, ViewChild } from '@angular/core';
import {Router} from '@angular/router';
import {AuthService} from './auth.service';
import {AppComponent} from '../app.component';
@Component({
selector: 'authorization',
templateUrl: './auth.component.html',
providers: [AuthService],
styleUrls: ['./auth.component.css']
})
export class AuthComponent {
user = {
email: '',
pass: ''
};
auth = {};
errorMessage = document.getElementsByClassName("error-message");
constructor(private authService: AuthService, private router: Router){}
@ViewChild(AppComponent, {static: false})
private appComponent: AppComponent;
comeIn(event:any) {
if (this.user.email !== '' && this.user.pass !== '') {
this.authService.getUser(this.user).subscribe(
(data: any) => {
this.auth = data;
console.log(this.appComponent.auth);
console.log(typeof this.appComponent.auth);
this.appComponent.auth = data;
console.log(this.appComponent.auth);
console.log(typeof this.appComponent.auth);
if (data)
this.router.navigate(['/marketer-cabinet']);
else {
this.errorMessage[0].classList.add("visible");
}
},
error => console.log(error)
);
}
}
hideErrorMes () {
this.errorMessage[0].classList.remove("visible");
}
}
Answer the question
In order to leave comments, you need to log in
in the child component, emit an event (EventEmitter)
and listen to this event in the parent
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question