How to change boolean value from parent component.ts to child component.ts want to show and hide the login and logout option using ngif but not working.I do not know how to set and share boolean value between two components. I am using templateUrl.
app.component.ts:
export class AppComponent {
public isValid:boolean;
constructor(private router:Router){
}
logout(){
localStorage.removeItem('email');
this.router.navigate(['./login']);
this.isValid=true;
}
}
login.component.ts
export class LoginComponent implements OnInit {
public username:string;
public password:string;
constructor(private router:Router) { }
ngOnInit() {
}
userLogin(form:NgForm){
if(form.value.username==="admin@gmail.com" && form.value.password==="admin")
{
localStorage.setItem('email',form.value.username);
this.router.navigate(['./php']);
this.isValid=false;//not working//
} } }
app.component.html
<ul class="nav navbar-nav navbar-right">
<li *ngIf="isValid">
<a [routerLink]="['/login']" >Login</a>
</li>
<li *ngIf="!isValid">
<a (click)="logout()">LogOut</a>
</li>
</ul>
I do not really understand what is login component designed for, as it is not used anywhere in code provided. Anyway I would strongly suggest to go through basics again. You need to learn more about parent-child component interaction. Here you can find tutorial for that: https://angular.io/guide/component-interaction
You must use
@Input
decorator you code is almost fine just add the followings sentence[isValid]="isLoginValid"
and@Input() isValid: any;
HTML App Component
child.component.ts
When passing data between components that lack a direct connection, such as siblings, grandchildren, etc, you should use a shared service. You could either use RXJS
BehaviorSubject
orSubject
for cross component communication.Subject vs BehaviorSubject
Create a Service
Register the service with an Angular module.
from Angular docs
Inject the SharedService in the components
app.component.ts:
login.component.ts