Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法以角度读取typescript中的属性_Angular_Typescript - Fatal编程技术网

Angular 无法以角度读取typescript中的属性

Angular 无法以角度读取typescript中的属性,angular,typescript,Angular,Typescript,我在typescipt中的类中创建了一个静态数组。但我看不清它的属性 错误如下所示 ERROR TypeError: Cannot read property 'forbiddenProjectNames' of undefined export class CustomValidator { private static forbiddenProjectNames = ['Test']; static forbiddenNames(control: FormControl): {

我在typescipt中的类中创建了一个静态数组。但我看不清它的属性

错误如下所示

ERROR TypeError: Cannot read property 'forbiddenProjectNames' of undefined
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;
    }
  }
下面是我的代码片段,如下所示

ERROR TypeError: Cannot read property 'forbiddenProjectNames' of undefined
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;
    }
  }

如何修复它?

您将其标记为
静态
,因此您可以这样引用它:
CustomValidator。禁止的项目名称
您将其标记为
静态
,因此您可以这样引用它:
CustomValidator。禁止的项目名称
我可以用这个来解决它

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

我可以用这个来解决它

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

为什么将属性和方法标记为
静态
?至少对于属性而言,它没有意义,因为您仍然将其标记为私有,因此不希望从外部对其进行恢复。您的代码运行正常。为什么将属性和方法标记为
静态
?至少对于属性而言,它没有意义,因为您无论如何都会将其标记为私有,因此您不想从外部对其进行恢复。您的代码运行良好即使他使用此关键字,其运行良好即使他使用此关键字,其运行良好