gpt4 book ai didi

javascript - Knockout JS 将参数传递给函数

转载 作者:行者123 更新时间:2023-11-28 01:11:12 24 4
gpt4 key购买 nike

我正在开发 Durandal 项目,我需要知道从 HTML 端调用传递参数的函数的最佳方法。在以前的版本中我总是使用这种方式:

<div data-bind="foreach: rows">
<div data-bind="visible: $root.myFunction.bind($data,'parameter')">
...
...
</div>
</div>

但在这种情况下,在 Js 端,在我的函数中,我只接收主要对象:我无法接收我的 ROW 数据,也无法接收“参数”值!这是我在 js 端的模型:

define(['services/datacontext', 'durandal/app', 'config'], 
function (datacontext, app, config) {
var model = function () {
var self = this;
...
...
...
}

model.prototype.myFunction(parameter) {
var show = false;
...
...
return show;
}

return model;
}

你能帮我吗?

最佳答案

您可以在 myFunction 中以 this 的形式访问您的 $data - .bind 设置的第一个参数上下文(上下文 = this 引用的对象)。

   model.prototype.myFunction(parameter) {
var show = false;
// `this` is the $data you specified in your view
return show;
}

关于javascript - Knockout JS 将参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24408617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com