事件自行触发
Event triggers by itself
我不知道为什么只有当她自己按下按钮 运行 时功能才应该 运行。
这是我的按钮声明:
var oButton = new sap.m.Button({
id: "buttonId",
text: "Yes",
press: this.fnB()
});
我的控制器如下所示:
sap.ui.controller("<controller-name>", {
fnA: function(){<button_declaration_here>},
fnB: function(){console.log("Hello from fnB!");}
});
当我 运行 我得到的应用程序时:
Hello from fnB!
Uncaught TypeError: Cannot read property '0' of undefined
还没有按下任何按钮,为什么我会收到问候消息?
如果重要的话,我会使用 SAP WEB IDE...
这个:
press: this.fnB()
调用 this.fnB()
并使用它的 return 值来初始化 press
属性,就像 [=17] =] 调用 foo
并将其return值赋给x
.
你可能想要
press: this.fnB
或
press: this.fnB.bind(this)
因此您将函数的引用分配给 press
,而不是调用它并使用它的 return 值。
第二个例子可能需要一些解释:如果我们只使用
press: this.fnB
会将函数分配给 press
,但是当它是 运行 时,调用 fnB
期间的 this
与上面代码中的this
,因为在JavaScript中,函数中this
的值通常由函数的调用方式决定。
使用Function#bind
:
press: this.fnB.bind(this)
...创建一个 new 函数,调用时,将使用我们赋予 bind
的 this
值调用原始函数。
如果您想在 SAPUI5 中使用控制器的功能,您应该使用:
press: [this.fnB, oController]
在此声明之后 this
指的是控制器而不是事件
我不知道为什么只有当她自己按下按钮 运行 时功能才应该 运行。
这是我的按钮声明:
var oButton = new sap.m.Button({
id: "buttonId",
text: "Yes",
press: this.fnB()
});
我的控制器如下所示:
sap.ui.controller("<controller-name>", {
fnA: function(){<button_declaration_here>},
fnB: function(){console.log("Hello from fnB!");}
});
当我 运行 我得到的应用程序时:
Hello from fnB!
Uncaught TypeError: Cannot read property '0' of undefined
还没有按下任何按钮,为什么我会收到问候消息?
如果重要的话,我会使用 SAP WEB IDE...
这个:
press: this.fnB()
调用 this.fnB()
并使用它的 return 值来初始化 press
属性,就像 [=17] =] 调用 foo
并将其return值赋给x
.
你可能想要
press: this.fnB
或
press: this.fnB.bind(this)
因此您将函数的引用分配给 press
,而不是调用它并使用它的 return 值。
第二个例子可能需要一些解释:如果我们只使用
press: this.fnB
会将函数分配给 press
,但是当它是 运行 时,调用 fnB
期间的 this
与上面代码中的this
,因为在JavaScript中,函数中this
的值通常由函数的调用方式决定。
使用Function#bind
:
press: this.fnB.bind(this)
...创建一个 new 函数,调用时,将使用我们赋予 bind
的 this
值调用原始函数。
如果您想在 SAPUI5 中使用控制器的功能,您应该使用:
press: [this.fnB, oController]
在此声明之后 this
指的是控制器而不是事件