I have a custom component that has an *ngIf in its view and receives a boolean variable, but *ngIf doesn't work. This is the code:
Component
@Input('title') titleText;
@Input('backButton') backButton;
@Input('avatarImage') avatarImage;
@Input('userId') userId;
@Output('avatarClicked') avatarClicked = new EventEmitter()
titlePage: string;
img: string;
back: boolean = false;
constructor() {}
ngAfterViewInit() {
this.titlePage = this.titleText;
this.img = this.avatarImage;
this.back = this.backButton;
}
openPersonalData() {
this.avatarClicked.emit({value: this.userId})
}
Component HTML
<ion-header>
<ion-toolbar>
<ion-buttons *ngIf="back == true" slot="start">
<ion-back-button text="Voltar"></ion-back-button>
</ion-buttons>
<ion-title>{{ titlePage }}</ion-title>
<ion-avatar slot="end" (click)="openPersonalData()">
<img [src]="img">
</ion-avatar>
</ion-toolbar>
</ion-header>
Page using the component
<app-header title="Chat"
[backButton]="true"
avatarImage="{{ user[0].img }}"
userId="{{ user[0].id }}"
(avatarClicked)="openPersonalData($event)">
</app-header>
Does anyone know what I'm doing wrong?
This code below works right, with the logic you need:
You can check it here: (you only need to change the value of [backButton]="true" to [backButton]="false" to see it showing/not showing the son)
FATHER HTML:
<app-header
title="If you see this, backButton is set to true"
[backButton]="true"
>
</app-header>
SON HTML:
<div *ngIf="backButton">
{{ title }}
</div>
SON COMPONENT TS:
import { Component, Input } from "@angular/core";
@Component({
selector: "app-header",
templateUrl: "./header.component.html",
styleUrls: []
})
export class HeaderComponent {
@Input("title") title;
@Input("backButton") backButton;
}