模型 属性 在 formatOptions 中被视为对象而不是字符串

Model property treated as object instead of string in formatOptions

我有一个包含标签和输入组件的片段。它的值大部分是静态设置的,并且按预期工作:

<Label text="Customer" />
<Input
  value="John Smith"
  editable="true"
  change=".liveChangeVehicleGrossPrice"
/>
<Label text="Price" />
<Input
  editable="true"
  change=".liveChangeVehicleGrossPrice"
  value="{
    parts: [
      'P02_Model>/VehicleGrossPrice',
      'valuta>/convert'
    ],
    type: 'sap.ui.model.type.Currency',
    formatOptions: {
      showMeasure: false
    }
  }"
/>

现在我在控制器中创建了一个模型,其中包含要在输入组件中使用的值:一个要添加到客户名称字段中的值(客户)和两个属性(groupingSeparatordecimalSeparator) 用于格式化货币:

var json = {
  "groupingSeparator": " ",
  "decimalSeparator": ",",
  "customer": "John Wayne",
};
var model = new JSONModel(json);
this.getView().setModel(model, "P02_Model");

创建此模型后,我修改片段以使用这些值:

<Label text="Customer" />
<Input
  editable="true"
  change=".liveChangeVehicleGrossPrice"
  value="{P02_Model>/customer}"
/>
<Label text="Price" />
<Input
  editable="true"
  change=".liveChangeVehicleGrossPrice"
  value="{
    parts: [
      'P02_Model>/VehicleGrossPrice',
      'valuta>/convert'
    ],
    type: 'sap.ui.model.type.Currency',
    formatOptions: {
      showMeasure: false,
      groupingSeparator: {P02_Model>/groupingSeparator},
      decimalSeparator: {P02_Model>/decimalSeparator}
    }
  }"
/>

问题是加载页面时,名称 John Wayne 正确映射到关联的输入中,但包含货币的输入将具有

112[Object object]323[Object object]2

而不是 112 323,2

groupingSeparatordecimalSeparator 相关联的两个字符串值以某种方式被视为对象。为什么?

WHY?

这是因为绑定信息对象不是 ManagedObject 而是一个不支持绑定功能的简单对象。你必须在 JS 中完成。

<Input id="myInput" />
onInit: function() {
  // ...    
  const model = this.getView().getModel("P02_Model");
  this.byId("myInput").bindValue({
    parts: [
      "P02_Model>/VehicleGrossPrice",
      "valuta>/convert"
    ],
    type: this.createCurrencyType()
  });
},

onModelDataChanged: function() {
  this.byId("myInput").getBinding("value").setType(this.createCurrencyType());
},

createCurrencyType: function() {
  return new CurrencyType({ // required "sap/ui/model/type/Currency"
    groupingSeparator: model.getProperty("/groupingSeparator"),
    decimalSeparator: model.getProperty("/decimalSeparator")
  });
},