javascript Knockout.js - xxxx 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7487180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:16:46  来源:igfitidea点击:

Knockout.js - xxxx is not a function

javascriptjqueryknockout.js

提问by BonyT

I am trying to incorporate Knockout.js into a WebApplication.

我正在尝试将 Knockout.js 合并到 WebApplication 中。

The tutorial I am basing much of my code on is here.

我的大部分代码所基于的教程在这里

Basically - I have a list of items - I want to be able to click an item and have the corresponding data appear in a div at the bottom of the page. Eventually I'll use jquery.UI Dialog plugin to turn this div into a popup, but for now, I'm just trying to get the selectedItem to work.

基本上 - 我有一个项目列表 - 我希望能够单击一个项目并将相应的数据显示在页面底部的 div 中。最终我将使用 jquery.UI Dialog 插件将这个 div 变成一个弹出窗口,但现在,我只是想让 selectedItem 工作。

My (simplified) code is here: http://jsfiddle.net/fZXAX/1/

我的(简化)代码在这里:http: //jsfiddle.net/fZXAX/1/

I just get the error: actionListViewModel.selectedActionId is not a function.

我刚刚收到错误: actionListViewModel.selectedActionId is not a function

I don't see the difference between this and the tutorial which uses selectedMailId in an identical way. The only difference between my code and the example is that I am not using literal notation.

我没有看到这与以相同方式使用 selectedMailId 的教程之间的区别。我的代码和示例之间的唯一区别是我没有使用文字符号。

Can anyone see where I am going wrong? Thanks in advance.

谁能看到我哪里出错了?提前致谢。

回答by Skilldrick

Your error is here:

你的错误在这里:

click: function() {actionListViewModel.selectedActionId(id)}

actionListViewModelis a constructor function, but you're acting as if it's an object.

actionListViewModel是一个构造函数,但你表现得好像它是一个对象。

See this forked jsFiddle. This line, which defines a constructor function

看到这个分叉的 jsFiddle。这一行,定义了一个构造函数

function actionListViewModel () {

was changed to be an instance of a new object, created by calling an anonymous constructor function.

被更改为通过调用匿名构造函数创建的新对象的实例。

var actionListViewModel = new function () {

and this line, where you were creating an instance of your previously defined function

以及这一行,您在其中创建了先前定义的函数的实例

ko.applyBindings(new actionListViewModel());

was changed to just pass in the instance that we setup earlier

改为只传入我们之前设置的实例

ko.applyBindings(actionListViewModel);

Alternatively you could simply define a variable viewModeland set it = new actionListViewModel();and then update your click literal to point to viewModelinstead of actionListViewModel. You can see that approach here

或者,您可以简单地定义一个变量viewModel并设置它= new actionListViewModel();,然后更新您的点击文字以指向viewModel而不是actionListViewModel. 你可以在这里看到这种方法