如何在 XML 视图中动态加载 XML 片段? [英] How to dynamically load an XML fragment in XML view?

查看:26
本文介绍了如何在 XML 视图中动态加载 XML 片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下 XML 视图:

Suppose I have the following XML view:

<mvc:View xmlns:mvc="sap.ui.core.mvc" ...>
    <Page>
        <content>
            <l:VerticalLayout>
                <l:content>
                    <core:Fragment fragmentName="my.static.Fragment" type="XML" />
                </l:content>
            </l:VerticalLayout>
        </content>
    </Page>
</mvc:View>

片段 my.Fragment 是静态加载的.但是,我现在想动态更改要加载的片段(理想情况下使用数据绑定 fragmentName 属性,但任何其他方式也应该可以),即.像这样:

The fragment my.Fragment is statically loaded. However, I now want to dynamically change the to-be-loaded fragment (ideally using data binding the fragmentName property, but any other means should be ok as well), ie. something like this:

<mvc:View xmlns:core="sap.ui.core.mvc" ...>
    <Page>
        <content>
            <l:VerticalLayout>
                <l:content>
                    <core:Fragment fragmentName="{/myDynamicFragment}" type="XML" />
                </l:content>
            </l:VerticalLayout>
        </content>
    </Page>
</mvc:View>

但是,后者不起作用,并且 Fragment 定义不允许数据绑定......我可能错过了一些东西,但是我应该如何根据参数/模型属性动态更改 XML 视图中的 Fragment/等?

However, the latter does not work, and the Fragment definitions don't allow for data binding... I might have missed something, but how should I dynamically change the Fragment in my XML view based on a parameter/model property/etc?

目前,我已经求助于自定义控件,而不是在我的视图中直接使用片段,并让该控件将分派到适当的片段,但我觉得应该有一个更简单的-盒子方式...

推荐答案

我认为唯一的解决方案是从控制器的 onInit 方法初始化片段:

I think the only solution will be initialization of fragment from onInit method of controller:

sap.ui.controller("my.controller", {
    onInit : function(){
        var oLayout = this.getView().byId('mainLayout'), //don't forget to set id for a VerticalLayout
            oFragment = sap.ui.xmlfragment(this.fragmentName.bind(this));
        oLayout.addContent(oFragment);
    },

    fragmentName : function(){
       return "my.fragment";
    }
});

这篇关于如何在 XML 视图中动态加载 XML 片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆