您可以在实例化片段时关联一个唯一的 ID。这样,这个唯一的 ID 将以片段包含的控件的 ID 为前缀。
因此,两个不同的代码将是:
onRequestNewAddCustomerPress: function(oEvent) {
if (!this.oAddCustomerDialog) {
this.oAddCustomerDialog = sap.ui.xmlfragment("idOnNewRequest","com.sap.lccapp.fragment.AddCustomer", this);
}
this.oAddCustomerDialog.openBy(oEvent.getSource());
},
进而:
onVisitEditAddCustomerPress: function(oEvent) {
if (!this.oAddCustomerDialog) {
this.oAddCustomerDialog = sap.ui.xmlfragment("idOnEdit","com.sap.lccapp.fragment.AddCustomer", this);
}
this.oAddCustomerDialog.openBy(oEvent.getSource());
},
另外,请检查以下文档主题:声明性 XML 或 HTML 片段中的 ID
编辑:如果从两个不同的视图调用这些片段,最好使用视图的 ID。我将修改代码以实例化片段,如下所示:
this.oAddCustomerDialog = sap.ui.xmlfragment(this.getView().getId(), "com.sap.lccapp.fragment.AddCustomer", this);
从UI5 1.58开始,工厂功能sap.ui.*fragment
已弃用。请用Fragment.load反而!
Fragment.load({
id: this.getView().getId(),
name: "com.sap.lccapp.fragment.AddCustomer",
controller: this,
}); // returns a promise