代码之家  ›  专栏  ›  技术社区  ›  Michu93

在OpenUI5中设置和获取模型值

  •  1
  • Michu93  · 技术社区  · 6 年前

    我的代码:

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

    上面说 name null .

    1 回复  |  直到 6 年前
        1
  •  2
  •   Erch    6 年前

    https://sapui5.hana.ondemand.com/1.54.8/#/topic/e5310932a71f42daa41f3a6143efca9c

    但要快速回答: / ,你不需要 " 在你的json里

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

    另外,为了使您的代码更易于阅读,我将按以下几行进行smth:

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