不能在我的事件处理程序中访问视图。"this.getView不是一个函数" [重复]

问题描述 投票:0回答:1

我打算做一个简单的表单,在我的SAPUI5应用程序中通过OData服务创建新条目。我的方法是制作一个Dialog,如下所示。

<Dialog id="CreateDialog" title="Add New Employee">
  <Button id="bt" text="s"/>
  <form:Form xmlns:form="sap.ui.layout.form" editable="true">
    <form:FormContainer title="Title">
      <form:FormElement label="Employee ID">
        <Input id="txtEmpid" width="100%" placeholder="Emp ID" />
      </sap.ui.layout.form:FormElement>
    </form:FormContainer>
    <form:layout>
      <form:ResponsiveGridLayout />
    </form:layout>
  </form:Form>
  <beginButton>
    <Button id="_btnSubmit" text="Submit" press=".submitDialog" />
  </beginButton>
</Dialog>

我创建了一个简单的表单,其中有一个按钮给定 id="bt" 输入文字 id ="txtEmpid". 我的问题是,我不能达到这个ID在我的控制器。

我是这样尝试的。我加了一个按钮 press=".submitDialog" 并在我创建整个视图的控制器中实现该方法(通过ID获取控件并简单地更改文本)。

sap.ui.define([
  "sap/ui/core/mvc/Controller"
], function (Controller) {
  "use strict";

  return Controller.extend("Demo1.controller.InitView", {
    showCreateDialog: function () {
      var view = this.getView();
      var createDialog = view.byId("CreateDialog");
      var oDummyController = {
        // This is when I clicked the Submit button in dialog
        submitDialog: function () {
          var button = this.getView().byId("bt");
          var inputText = this.getView().byId("txtEmpid");
          // I tried to test if I reached these controls by byId method
          button.setText("ssss");
          inputText.setValue("zzz");
        },
        closeDialog: function () {
          createDialog.close();
        }
      };
      // This is when the dialog event is fired, things are fine here
      if (!createDialog) {
        createDialog = sap.ui.xmlfragment(view.getId(), "Demo1.view.CreateDialog", oDummyController);
      }
      view.addDependent(createDialog);
      createDialog.open();
      if (!createDialog.isOpen()) {
        //do sth
      }
    }
  });
});

我试着看看会发生什么,这是错误的。

sapui5 screenshot error

我无法访问控件。

sapui5
1个回答
1
投票

你的代码中已经解决了这个问题。你只是看不到它。

在你的 "submitDialog "中,"this "不是指你所期望的。如果你用view.byId()--你已经定义了--来解决这个问题,可能就会成功,因为当你定义 "view "变量的时候,你同时得到了正确的 "this "引用。通常这不是推荐的方法。我们大多数人都是这样做的,像.NET一样。

var that = this;

然后像这样使用

showCreateDialog: function() {
    var that = this;

    var oDummyController = {
        submitDialog: function() {
            var button = that.getView().byId("bt");
© www.soinside.com 2019 - 2024. All rights reserved.