Can't bind property from model to control on XML view

半城伤御伤魂 提交于 2021-02-08 10:40:20

问题


I trying to migrate my app on new version of Openui5 (1.48) and have some problems with model bindings. I am using sap.ui.getCore().setModel(oModel, "local") for model declaration and when I trying to bind some controls to values from this model like this:

<Text text="{local>/count}"/>

value isn't displayed but if I will get this model, set it to view in controller and remove > from xml

var oModel = sap.ui.getCore().getModel("local");
this.getView().setModel(oModel);

XML

<Text text="{/count}"/>

everything would work fine. Maybe somebody had faced with similar problem or has an idea was wrong with my code?

EDIT

I solved the problem with solution suggested by @boghyon

new sap.ui.core.ComponentContainer({
    //...,
    propagateModel: true
})

回答1:


You must be using a Component in your app. In that case, core models are not automatically propagated to the children of the ComponentContainer which is why your Text control doesn't know the model "local".

The reason why "{/count}" works is because you set the model explicitly on the view without any model name. If the model doesn't have a name, it's a default model and > has to be omitted in the binding path.

To learn more about where to set models, take a look at my answer to a similar question: https://stackoverflow.com/a/42251431/5846045




回答2:


I think problem may be how you creating the JSON model!,

try this one. Controller

sap.ui.define(["sap/ui/core/mvc/Controller",
               "sap/ui/model/json/JSONModel",],
               function(Controller,JSONModel) {
    "use strict";
    return Controller.extend("com.stackoverflow.testUI5", {

        onInit:function(){

            var oData = {
                    count:"1"
            };
        var oModel = new JSONModel(oData);
            sap.ui.getCore().setModel(oModel , "local")
            //this.getView().setModel(oModel ,"local");

        }
});
});

XML View

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<mvc:View controllerName="com.stackoverflow.testUI5"
     xmlns:mvc="sap.ui.core.mvc" 
    xmlns:core="sap.ui.core"  xmlns="sap.m" >

            <Text text="{local>/count}"/>

</mvc:View>

this snippet will work.



来源:https://stackoverflow.com/questions/46894283/cant-bind-property-from-model-to-control-on-xml-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!