簡體   English   中英

在OpenUI5中設置和獲取模型值

[英]Setting and getting model values in OpenUI5

我正在嘗試使用OpenUi5。 我要設置“人工模型”以更改值並打印。 我的代碼:

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);

它說該namenull 為什么會這樣?

您可能想從SAP看一下本教程: https : //sapui5.hana.ondemand.com/1.54.8/#/topic/e5310932a71f42daa41f3a6143efca9c

但對於一個快速的答案:你缺少一個/和你不需要的"在你的JSON

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

與設置的屬性行相同

另外,為了使您的代碼更易於閱讀,我會在以下方面做一些工作:

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