如何防止使用参数在页面加载上被调用?
我有一个单击按钮后调用的函数。 现在看来,
<button class=".." data-bind="click: $root.openModal">
我正在尝试将参数传递给此功能。导致此代码
<button class=".." data-bind="click: $root.openModal(Object.Keys(params))">
参数将成功传递,但是现在,每当我加载页面时,即使在单击按钮之前,都调用了Open -Modal函数。即使我离开'()'而不是 openmodal(object.keys(params)),也会发生同样的情况。
函数本身,看起来这样,
self.openModal = function (keys) {
popupObservable(true);
self.modal = $.openModalDefault({
url: '#js-confirmation-dialog-template',
className: 'doc',
onLoad: function () {
popupObservable(false);
if (!$(selectors.confirmationModal)[0]) {
return;
}
var viewModel = new ConfirmationDialogViewModel(function () {
self.confirm(keys);
self.modal.close();
}, "This part is irrelevant");
ko.applyBindings(viewModel, $(selectors.confirmationModal)[0]);
}
});
};
OpenModal和OpenModal()有什么区别,如何将参数传递给此函数而不在页面加载上触发它?
I have a function that is called after button is clicked. it looks like this
<button class=".." data-bind="click: $root.openModal">
Now I am trying to pass parameters to this function. Resulting in this code
<button class=".." data-bind="click: $root.openModal(Object.Keys(params))">
Parameters are passed successfully, but now whenever I load the page, before even clicking the button, openModal function is called. Same happens even if I leave '()' instead of openModal(Object.Keys(params)).
function itself, looks like this
self.openModal = function (keys) {
popupObservable(true);
self.modal = $.openModalDefault({
url: '#js-confirmation-dialog-template',
className: 'doc',
onLoad: function () {
popupObservable(false);
if (!$(selectors.confirmationModal)[0]) {
return;
}
var viewModel = new ConfirmationDialogViewModel(function () {
self.confirm(keys);
self.modal.close();
}, "This part is irrelevant");
ko.applyBindings(viewModel, $(selectors.confirmationModal)[0]);
}
});
};
What is the difference between openModal and openModal() and how do I pass parameters to this function without triggering it on page load?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是因为您在
data-bind =“单击:$ root.openmodal(object.keys(params))上调用函数”
您想用这些参数动态构建功能并执行该功能点击。That is because you are invoking the function on
data-bind="click: $root.openModal(Object.Keys(params))"
instead you want to construct a function dynamically with those parameters and execute that on click.另外,您只需将函数传递到单击处理程序时即可传递:
&lt; button data-bind =“ click:()=&gt; $ root.openmodal(object.keys(params))”&gt;
我个人更喜欢,因为这意味着您不必复制
Open -Modal
函数。Alternatively, you could just pass a function literal as the click handler:
<button data-bind="click: () => $root.openModal(Object.Keys(params))">
Which I would personally prefer because it means you don't have to duplicate your
openModal
function.