0

I am trying to get the input value, but when I call the function I get the error this.getView() is not a function

Below is the function in controller

    handleConfirmationMessageBoxPress: function(oEvent) {
        var bCompact = !!this.getView().$().closest(".sapUiSizeCompact").length;
        MessageBox.confirm(
            "Deseja confirmar a transferência?", {
                   icon: sap.m.MessageBox.Icon.SUCCESS,
                   title: "Confirmar",
                    actions: [sap.m.MessageBox.Action.OK, sap.m.MessageBox.Action.CANCEL],
                    onClose: function(oAction) {
                      if (oAction == "OK"){
                          var loginA = this.getView().byId("multiInput").getValue();
                          alert(loginA)
                          MessageToast.show("Transferência efetuada");

                      }else{
                         // MessageToast.show("Transferência não cancelada");
                           }

                        },
                        styleClass: bCompact? "sapUiSizeCompact" : ""
            }
        );
    }

And here is the input in the view

   <m:Input id="multiInput" value="teste" placeholder="Clique no botão ao lado para buscar o usuário" showValueHelp="true" valueHelpRequest="valueHelpRequest" width="auto"/>
4

1 回答 1

3

我会假设您第二次this.getView()从回调内部收到该错误。你得到这个是因为thisJavaScript 的工作方式。请参阅以下 MDN 文档:https ://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/this 。

简而言之,在没有从对象“内部”引用的情况下自由调用函数(即fnFunction()vs oObject.func()),将导致thisto 指向空对象或窗口对象。要获得正确的this,您可以使用箭头函数声明、jQuery.proxy方法或.bind函数:

onClose: oAction => {
   // your code
}

// OR

onClose: function(oAction) {
   // your code
}.bind(this)

// OR

onClose: jQuery.proxy(function(oAction) {
   // your code
}, this)
于 2017-05-03T15:01:22.900 回答