Javascript 角度9-错误类型错误:无法读取属性';名称';未定义的

Javascript 角度9-错误类型错误:无法读取属性';名称';未定义的,javascript,angular,angular-material,Javascript,Angular,Angular Material,我试图创建一个动态表单控件,我希望根据条件禁用一些控件,但我得到类型错误undefined。 这是我的代码 ngOnInit(): void { this.dynamicForm = this.formBuilder.group({ tickets: new FormArray([]) }); } get f() { return this.dynamicForm.controls; } get t() { return this.f.tickets as For

我试图创建一个动态表单控件,我希望根据条件禁用一些控件,但我得到类型错误undefined。 这是我的代码

ngOnInit(): void {

 this.dynamicForm = this.formBuilder.group({
      tickets: new FormArray([])
  });
  }
  get f() { return this.dynamicForm.controls; }
  get t() { return this.f.tickets as FormArray; }

       itemClick(node) {

for (let i = 0 ; i < this.data_tree.length; i++) {
         this.nestsort.push(Object.assign({}, ...function _flatten(o) { return [].concat(...Object.keys(o).map(k => typeof o[k] === 'object' ? _flatten(o[k]) : ({[k]: o[k]})))}(this.data_tree[i])))
      var check
         rights.forEach(element => {
          if(element == this.temp[i].id)
          {
           check = false
          }
          else{
            check = true;
          }


        });
         this.t.push(this.formBuilder.group({
          name: ({value: [this.nestsort[i][aisa]], disabled: check})   

  }));      




         this.temp1 = this.t.value;


     }


} 

尝试在模板中访问
temp1
变量时,可能未定义该变量。您可以使用
?。
来解决此错误



在尝试访问其属性之前,它会检查是否定义了
temp1[i]

使用“Elvis操作符”。保护属性路径中的未定义值和空值非常有用。该操作符允许我们在不知道路径是否存在的情况下导航对象路径。如果对象路径存在,则返回该路径的值,否则返回空值。防止空引用异常非常有用


代替


可能是因为当呈现模板时,temp1对象还不存在,然后快速建议,在所有需要检查temp1[i]是否存在的情况下,尝试将条件更改为该对象。名称:
*ngIf=“temp1&&temp1[i]&&temp1[i].name”

然后,如果temp1还不存在,您不会得到任何错误:)。

它不会解决我的问题,因为它不会呈现任何输入字段,那么条件
temp1[i]?.name
应该有问题。包括安全导航操作员仅确保定义了
temp1[i]
。因此,如果需要通过条件,则需要确保定义了
temp1[i]
<div *ngFor="let ticket of t.controls; let i = index">
              <div [formGroup]="ticket">
                  <div>
                      <mat-form-field *ngIf="temp1[i].name" appearance="outline"  style="width: 95%;">
                        <mat-label>{{temp[i].id}} </mat-label>
                      <input matInput type="text" placeholder="Name"  formControlName="name" class="form-control"/>
                      <!-- <mat-hint >Errors appear instantly!</mat-hint> -->
                    </mat-form-field>
navbars.component.html:77 ERROR TypeError: Cannot read property 'name' of undefined
    at Object.updateDirectives (navbars.component.html:79)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:46970)
    at checkAndUpdateView (core.js:45981)
    at callViewAction (core.js:46347)
    at execEmbeddedViewsAction (core.js:46304)
    at checkAndUpdateView (core.js:45982)
    at callViewAction (core.js:46347)
    at execEmbeddedViewsAction (core.js:46304)
    at checkAndUpdateView (core.js:45982)
    at callViewAction (core.js:46347)