absence
absence

Reputation: 358

Control with bound property doesn't display model data in the UI

I'm migrating my app to new version of OpenUI5 (1.48) and have some problems with model bindings. I am using sap.ui.getCore().setModel(oModel, "myModel") for model declaration and when I'm trying to bind some controls to values from this model like this ...

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

... the value isn't displayed.

But if I get this model, set it to view in controller ...

var oModel = sap.ui.getCore().getModel("local");
this.getView().setModel(oModel);
<Text text="{/count}" />

... everything would work fine. Maybe somebody faced a similar problem or has an idea what is wrong with my code?

Upvotes: 1

Views: 2595

Answers (2)

Boghyon Hoffmann
Boghyon Hoffmann

Reputation: 18054

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

Upvotes: 1

Harish CHH
Harish CHH

Reputation: 104

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.

Upvotes: 1

Related Questions