0

I want to get errors message in function of component instead show them in template:

<p [hidden]="!name.control.errors.required" *ngIf="name?.control?.errors?.required">name is required</p>

How to get errors message of form in function of Component?

1 Answer 1

1

If you have access to the control associated to the input, you can do the same thing:

@Component({
  template: `
    <form>
      <input [ngFormControl]="control"/>
    </form>
    <div (click)="showError()">Show error</div>
  `
})
export class MyComponent {
  constructor() {
    this.control = new Control('', Validators.required);
  }

  showError() {
    if (this.control.errors.required) {
      // Have the required error
    }
  }
}
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.