简体   繁体   English

在OpenUI5中设置和获取模型值

[英]Setting and getting model values in OpenUI5

I am trying to play with OpenUi5. 我正在尝试使用OpenUi5。 I want to set 'artificial model' change the value and print it. 我要设置“人工模型”以更改值并打印。 My code: 我的代码:

onInit: function () {
    this.getView().setModel(new sap.ui.model.json.JSONModel());
    this.getView().getModel().setData({"name":"Jon"});
    this.getView().getModel().setProperty("name", "Ann");

    var name = this.getView().getModel().getProperty("name");
    window.alert(name);

It says that name is null . 它说该namenull Why is that so? 为什么会这样?

you might want to look into this tutorial from SAP: https://sapui5.hana.ondemand.com/1.54.8/#/topic/e5310932a71f42daa41f3a6143efca9c 您可能想从SAP看一下本教程: https : //sapui5.hana.ondemand.com/1.54.8/#/topic/e5310932a71f42daa41f3a6143efca9c

but for a quick answer: you are missing a / , and you dont need the " in you json 但对于一个快速的答案:你缺少一个/和你不需要的"在你的JSON

this.getView().getModel().setData({name:"Jon"});
...
var name = this.getView().getModel().getProperty("/name");

obv same for the set property line 与设置的属性行相同

also for easier readability of you code i'd do smth in the lines of: 另外,为了使您的代码更易于阅读,我会在以下方面做一些工作:

onInit: function () {
    var oYourModel = new JSONModel({
            name: "Jon"
    });
    this.getView().setModel(oYourModel, "modelName");
    this.getView().getModel("modelName").setProperty("/name", "Ann");

    var name = this.getView().getModel().getProperty("/name");
    window.alert(name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM