2

我想在按钮按下时将值从视图传递给控制器​​。在视图中传递的是一个对象 productJson。但我无法在控制器中检索该值。请帮忙。

查看js:

 new sap.m.Button({
         text: "Add to Cart",
         press:[oController.addtoCartBtnPress,oController,productJson],
        })

控制器js:

 addtoCartBtnPress:function(e,oView,productJson)
      {
      }

结果:

oView and productJson values are returned as undefined.
4

1 回答 1

9

数据应该是 press 数组中的第一个值。根据sap.m.Button的 sdk文档:

按:fnListenerFunction 或 [fnListenerFunction, oListenerObject] 或 [oData, fnListenerFunction, oListenerObject]

监听器函数应该有 2 个参数: 1-事件;2-数据。

onPressFn: function(evt, data) { ... }

要获得视图,只需使用:

var view = this.getView();
  • "this" 将等于您作为 press 数组中的第三个值传递的任何值,并且通常应该是控制器以匹配 xml/html 视图的行为。

在新闻调用中传递数据的另一种方法是使用视图模型绑定,特别是如果您已经在视图的其他地方使用了该模型绑定。但这取决于你有多少产品和其他因素,所以我不会认为它对你的情况很理想。

//in the view
var productModel = new sap.ui.model.json.JSONModel(productJson);
view.setModel(productModel, "product");

//in the controller:
var data = view.getModel("product").getData();
于 2013-12-31T21:24:37.100 回答