什么时候在数据绑定中使用大括号?

When to use curly bracket in data binding?

在openui5的demo kit中,Element Binding一节涉及两种数据绑定实现形式

如以下代码所示:

var oMatrixLayout = new sap.ui.commons.layout.MatrixLayout();
oMatrixLayout.bindElement("/company");
oMatrixLayout.createRow(
 new sap.ui.commons.Label({text: "Name:"}),
 new sap.ui.commons.TextField({value: "{name}"})
);

oMatrixLayout.createRow(
 new sap.ui.commons.Label({text: "Revenue:"}),
 new sap.ui.commons.TextField({value: "{revenue}"})
);
oMatrixLayout.createRow(
 new sap.ui.commons.Label({text: "Employees:"}),
 new sap.ui.commons.TextField({value: "{employees}"})
);

在指定文本字段的值时,我们使用大括号 ({}) 来指示该值来自模型。但是,在紧接此代码之后的代码中,没有使用大括号:

var data = {clients:[{firstName:"Donald", lastName:"Duck"}]};
...// create and set model here
var oLabel = new sap.ui.commons.Label("myLabel");
oLabel.bindProperty("text", "firstName");
oLabel.bindElement("/clients/0");

如您所见,oLabel.bindProperty("text", "firstName");
中没有使用大括号 那么,这两个例子有什么区别呢?何时在数据绑定中使用大括号,何时不使用?

什么时候使用括号?

在定义控件以及 属性 时使用方括号:(在运行时绑定)

示例:new sap.ui.commons.TextField({value: "{name}"})

和 XML <Label text="{name}"/>

什么时候不使用括号? // 稍后绑定

每当您使用 bindProperty 方法时。

阅读更多关于 property binding here