Reputation: 735
I have created a popup box component with error information (if there is an error).
At the moment I have an interface.
status-message.ts
export interface Status {
message: string;
}
login.component.ts
(parent)
import { Status } from '../interfaces/status-message';
export class LoginComponent implements OnInit {
statusFailed: boolean = false;
status: Status;
login() {
this.authService.login(formData).subscribe(
(res) => {
if (res.status === 'sucsess') {
console.log('success');
} else if (res.status = 'failed') {
this.statusFailed = true;
this.status.message = res.message
}
}
)
}
}
login.component.html
<app-info-box *ngIf="statusFailed" [ngClass]="{ 'error-box': statusFailed}" [status]="status"></app-info-box>
info-box.component.ts
(child)
import { Status } from '../interfaces/status-message';
export class InfoBoxComponent implements OnInit {
@Input() status: Status;
}
info-box.component.html
<span> {{status}} </span>
Whenever I run this I am getting an error, core.js:6014 ERROR TypeError: Cannot set property 'message' of undefined
and the child component shows no text, but shows the styling.
How can I get this working?
Thanks
Upvotes: 1
Views: 1486
Reputation: 3143
You are forgetting to initialize status
so when you try to do this.status.message = res.message
, it throws error because status
is still undefined.
A quick fix is to initialize status
like following in your login.component class:
status: Status = {
message: ''
}
Upvotes: 2