0

I created a static array in the class in typescipt. But I couldn't read its property.

The error is shown below.

ERROR TypeError: Cannot read property 'forbiddenProjectNames' of undefined

Here is my code snippet shown below.

export class CustomValidator {

  private static forbiddenProjectNames = ['Test'];

  static forbiddenNames(control: FormControl): {[s: string]: boolean} {
    if (this.forbiddenProjectNames.indexOf(control.value) !== -1) {
      return { 'nameIsForbidden': true };
    } else {
      return null;
    }
  }

How can I fix it?

2
  • Why do you mark the property and the method as static? At least for the property it does not make sense, because you mark it as private anyway, so you don't want to instatiate it from outside. Commented Oct 4, 2019 at 12:21
  • your code is working fine stackblitz.com/edit/angular-cnxev8 Commented Oct 4, 2019 at 12:25

2 Answers 2

2

You marked it as static, so you can refer to it like this: CustomValidator.forbiddenProjectNames

Sign up to request clarification or add additional context in comments.

1 Comment

its working fine in even if he use this keyword stackblitz.com/edit/angular-cnxev8
0

I can solve it by using this

 static forbiddenNames(control: FormControl): {[s: string]: boolean} {
    if (CustomValidator.forbiddenProjectNames.indexOf(control.value) !== -1) {
      return { 'projectNameIsForbidden': true };
    } else {
      return null;
    }
  }

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.