1

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

0

1 Answer 1

2

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: ''
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.