代码之家  ›  专栏  ›  技术社区  ›  AndreaNobili

为什么这个priming下拉列表没有用相关的表单控件值初始化?

  •  0
  • AndreaNobili  · 技术社区  · 5 年前

    我正在开发一个角度应用程序,下面的问题与如何在这个特定的情况下对一个预涂dropdwon进行估值有关,我对此感到非常疯狂。

    这是 ngOnInit() 我的组件的方法。

    ngOnInit(): void {
          
        this.loadEmployeesList().then(() => {this.loading = false;})
    
        this.assetService.currentAssetInfoMessage.subscribe(asset => {
          console.log("ASSET: ", asset);
    
          this.assetDetailsSelected = asset;
    
          this.assetDetailsForm = this.fb.group({
            asset_type: [this.assetDetailsSelected.asset_type, [Validators.required]],
            asset_model: [this.assetDetailsSelected.asset_model, [Validators.required]],
            employee_allocation: [null, [Validators.required]],
            asset_features: [this.assetDetailsSelected.asset_features, [Validators.required]],
            serial_number: [null, [Validators.required]],
            accessories: [null, [Validators.required]],
            allocation_date: [null, [Validators.required]],
            company: [null, [Validators.required]],
            notes: [null, [Validators.required]],
            invoice: [null, [Validators.required]]
          });
    
         if(this.assetDetailsSelected.employee_allocation) {
           console.log(this.assetDetailsSelected.employee_allocation[0].completeName);
    
           this.assetDetailsForm.setControl('employee_allocation',
                                             new FormControl(this.assetDetailsSelected.employee_allocation[0], Validators.required));
    
           
         }
          
        });
    
        
      }
    

    正如您所见,我首先检索了用于填充dropwown选项的员工列表(它工作正常)。然后我定义了一个包含一些字段的表单。注意:对于感兴趣的字段,我必须检查该字段是否为空,因为由于异步行为,它不会立即到达,所以为了不破坏我的应用程序,我做了以下工作:

     if(this.assetDetailsSelected.employee_allocation) {
       console.log("BLA");
       console.log(this.assetDetailsSelected.employee_allocation[0].completeName);
    
       this.assetDetailsForm.setControl('employee_allocation',
                                         new FormControl(this.assetDetailsSelected.employee_allocation[0], Validators.required));      
     }
    

    当 这已选择资产详细信息。员工分配 不是未定义的,它已正确打印,我在表单中添加了一个nre控件。

    然后,在这个组件的HTML代码中,我有一个包含以下内容的表单:

    <div class="row">
        <div class="col-2">
          <p>Assegnato a</p>
        </div>
        <div class="col-10">
          <p-dropdown id="employee_allocation"
                      [options]="employeesList$ | async" 
                      formControlName="employee_allocation"
                      placeholder="Impiegato" 
                      optionLabel="completeName" 
                      [showClear]="true">
          </p-dropdown>
        </div>
    </div>
    

    正如你所看到的,我正在使用 雇员名单$ 以填充选项。然后,我想在表格中显示设置的值,如下所示:

    this.assetDetailsForm.setControl('employee_allocation',
                                         new FormControl(this.assetDetailsSelected.employee_allocation[0], Validators.required));
    

    如你所见,我设置:

     formControlName="employee_allocation"
    

    指向包含该对象的窗体控件 这已选择资产详细信息。员工分配[0] 然后我指定这个对象必须使用的值是 完全名称 字段,按此设置:

     optionLabel="completeName" 
    

    问题在于,呈现表单时,未设置值:

    enter image description here

    正如您所见,它没有显示带有员工姓名的选定值。在Chrome控制台中,我得到这一行的输出: 安慰日志(this.assetDetailsSelected.employee_分配[0].completeName);

    是: 马里奥罗西 这是我想在下拉列表中设置的值。

    我的代码怎么了?我错过了什么?我该怎么修呢?

    0 回复  |  直到 5 年前
        1
  •  1
  •   sloth    5 年前

    确保将对象设置为窗体控件的值(在您的情况下: this.assetDetailsSelected.employee_allocation[0] )实际上在可能的选项列表中(在您的情况下: employeesList$ | async ).

    看起来像 employeesList$|异步 是字符串列表,但是 这已选择资产详细信息。员工分配[0] 看起来像一个JSON对象。

    也许你想

    this.assetDetailsForm.setControl('employee_allocation', new FormControl(this.assetDetailsSelected.employee_allocation[0].completeName, Validators.required));