Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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/8.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 尝试使用[(值)]的属性时出错_Angular_Typescript - Fatal编程技术网

Angular 尝试使用[(值)]的属性时出错

Angular 尝试使用[(值)]的属性时出错,angular,typescript,Angular,Typescript,我想选择存储在数据库中的选项字段 女的 男性的 私人实体:IprofileData; 异步ngOnInit{ this.entities=等待this.profileService.getEntities.pipetake1.toPromise; console.logthis.entities;//首先,您不能在模板中使用它 其次,不能在模板中使用私有属性,它们必须是公共的 第三,我建议你们坚持观察而不是承诺,这里有一个更好的解决方案 女的 男性的 实体$:可观察; 恩戈尼特{ this.en

我想选择存储在数据库中的选项字段

女的 男性的 私人实体:IprofileData; 异步ngOnInit{ this.entities=等待this.profileService.getEntities.pipetake1.toPromise; console.logthis.entities;//首先,您不能在模板中使用它

其次,不能在模板中使用私有属性,它们必须是公共的

第三,我建议你们坚持观察而不是承诺,这里有一个更好的解决方案

女的 男性的 实体$:可观察; 恩戈尼特{ this.entities$=this.profileService.getEntities.pipetake1; } 不要将值与formControlName一起使用,请使用formControlName

模板

<mat-form-field>
  <mat-select #gender formControlName="gender">
    <mat-option value="f">Female</mat-option>
    <mat-option value="m">Male</mat-option>
  </mat-select>
</mat-form-field>
组件技术

private entities: IprofileData;

async ngOnInit() {
  this.entities = await 
  this.profileService.getEntities().pipe(take(1)).toPromise();
  console.log(this.entities); // <-- This returns all the data that I need
  // you need to put the value into formGroup
  this.profileFormGroup.get('gender').patchValue(this.entities.gender);
}

您是否尝试过安全导航运算符?例如:this.entities?.gender.@R.Richards如果我这样做,我会得到错误“?.”运算符不能在分配中使用try just entities.gender没有。我尝试过这个。@R.Richards,但我仍然会得到错误,无法读取您在应用程序中使用的未定义属性“gender”?如果是,您需要设置t表单组中的默认值。我在[entities$|async as entities]中的第19列得到错误意外标记“as”。如果我删除as entities,我在[entities$|async=$event]中的第13列得到的操作表达式中就不能有管道