【问题标题】:Angular set value not working after upgrade升级后角度设置值不起作用
【发布时间】:2021-04-07 20:53:02
【问题描述】:

升级到最新版本的 angular(11) 和 typescript 4 后,表单的嵌套值的设置停止工作,有人可以帮助我吗?

之前有效但现在无效的线路是: control.at(+z).get('unitTotalPrice').setValue(totalUnitPriceFormatted, {onlySelf: true, emitEvent: false});

在一些编辑过的代码下面应该显示需要显示的所有内容

ngOnInit() 
  {
    .......
    this.myFormValueChanges$ = this.myorderForm.controls['itemRows'].valueChanges;
    this.myFormValueChanges$.subscribe(itemRows => this.updateTotalUnitPrice(itemRows));
        
        
        this.myorderForm = this.formBuilder.group({
          userName: [this.CurrentUserName],
          .....
          itemRows: this.formBuilder.array([this.initItemRows()]),
    
        })
        
        initItemRows() {
        const numberPatern = '^[0-9.,]+$';
        return this.formBuilder.group({
          sub_orderNumber : [''],
          .......
          unitTotalPrice: [''],
          ......
        },);
        .....
       }
       }
       
       updateTotalUnitPrice(itemRows: any) {
        const control = <FormArray>this.myorderForm.controls['itemRows'];
        ......
        for (let z in itemRows) {
          let totalUnitPriceFormatted = this.currencyPipe.transform(totalUnitPrice, 'GBP', '','1.2-2');
          control.at(+z).get('unitTotalPrice').setValue(totalUnitPriceFormatted, {onlySelf: true, emitEvent: false});
          console.log("what is totalUnitPriceFormatted here", totalUnitPriceFormatted)
          .......
          this.checkvalues();
        }
      }
      
      checkvalues(){
        let arr = <FormArray>this.myorderForm.controls.itemRows;
        console.log("what is arr here THIS IS THE WHOLE LINE", arr.value)
      }


the console.log in chrome says...
what is totalUnitPriceFormatted here xxx where xxx is the correct value

and
what is arr here THIS IS THE WHOLE LINE 
modified_date: "2021-04-06 16:22:08"
.......
unitTotalPrice: "0.00"
........

where uniTotalPrice contains the wrong information (0.00)

Please notice the checkvalues function is there just to check all the values of the array.

【问题讨论】:

    标签: angular typescript forms


    【解决方案1】:

    出于某种原因,我不得不将 onlySelf 的值从 true 更改为 false,我可能永远不知道为什么......但是你去...... 让 totalUnitPriceFormatted = this.currencyPipe.transform(totalUnitPrice, 'GBP', '','1.2-2'); control.at(+z).get('unitTotalPrice').setValue(totalUnitPriceFormatted, {onlySelf: false, emitEvent: false});

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-17
      • 2019-06-18
      • 1970-01-01
      • 2015-12-08
      • 2017-07-06
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多