简体   繁体   English

如何在SAPUI5 Controller中实现基于if / else的功能?

[英]How to Implement an if/else based in SAPUI5 Controller?

I am trying to do an if/else based on the checkbox flag. 我正在尝试基于复选框标志执行if / else。 Below are snippets of my view and controller. 以下是我的视图和控制器的摘要。 I am getting a syntax error on the last but one line. 我在最后一行上收到语法错误。 Anyone has any clue? 有人有任何线索吗?

View- 视图-

<mvc1:View
   controllerName="sap.ui.demo.wt.controller.App"
   xmlns="sap.m"
   xmlns:mvc1="sap.ui.core.mvc1">

<CheckBox id="i1" text="Test" selected="false" select ="checkDone" enabled="true" />

</mvc1:View>

Controller- 控制器-

sap.ui.define(["sap/ui/core/mvc/Controller"], function(Controller) {
    "use strict";
    return Controller.extend("sap.ui.demo.wt.controller.App", {
        checkdone: function() {
            var check = this.byId("i1").getSelected();
            if (check = true) {
                alert("Successful");
            } else {
                return '';
            }
        }
    })
};);

I assume that you need to remove return '' line. 我假设您需要删除return ''行。

Moreover, I would advice you to use if (check === true) or if (check) plus it seems to be more beneficial if you get selected checkbox by 此外,我建议您使用if (check === true)if (check)加上如果您通过以下方式选择复选框,则似乎更有利

checkDone: function (oEvent) {
     var bSelected = oEvent.getParameter('selected'));
}

If these hints don't help, check semicolons and brackets. 如果这些提示没有帮助,请检查分号和方括号。 In general every IDE should advice you something if it is wrong. 通常,如果错误,每个IDE都应向您提出建议。 I think that end of your controller should be like this: 我认为控制器的末端应如下所示:

            } else {
                return '';
            }
        }
    });
});

I have noticed some of the things in your code, 我注意到您代码中的某些内容,

Get the event in checkdone method and try to get the selected data from that event instead of id. 使用checkdone方法获取事件,然后尝试从该事件中获取选定的数据,而不是ID。

While comparing with the true use === instead of = . 与真实值进行比较时,请使用===而不是=

And in the last you are using the semicolon in the wrong places. 最后,您在错误的位置使用了分号 So thats why you are getting the errors. 这就是为什么您遇到错误的原因。

sap.ui.define(["sap/ui/core/mvc/Controller"], function (Controller) {
    "use strict";
    return Controller.extend("sap.ui.demo.wt.controller.App", {
        checkdone: function(oEvent){
            var check = oEvent.getParameter('selected');
            if (check === true){
                alert("Successful");
            } else {
                return '';
            }
        }
   });
});

As per your code you have used extra semicolon in last line 根据您的代码,您在最后一行使用了多余的分号

 sap.ui.define([ "sap/ui/core/mvc/Controller" ], function(Controller) {
     "use strict";
     return Controller.extend("sap.ui.demo.wt.controller.App", {
         checkdone: function() {
             var check = this.byId("i1").getSelected();
             if (check = true) {
                 alert("Successful");
             } else {
                 return '';
             }
         }
     })
 };);//extra semicolon 

Updated code: 更新的代码:

sap.ui.define(["sap/ui/core/mvc/Controller"], function(Controller) {
    "use strict";
    return Controller.extend("sap.ui.demo.wt.controller.App", {
        checkdone: function() {
            var check = this.byId("i1").getSelected();
            if (check) {
                alert("Successful");
            } else {
                return '';
            }
        }
    })
});//remove exta semicolon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM