KNOCKOUT.js问题:“H.apply不是一个函数.(在'h.apply(e,r)'中,'
knockout.js issues: "h.apply is not a function. (In 'h.apply(e,r)', 'h.apply' is undefined)"
所以我试图通过数据绑定在我的viewModel原型上调用一个方法。我将两个不同的元素数据绑定到同一方法,两者都通过"单击"。当我单击第一个按钮("新建游戏"按钮)时,它会显示我希望它显示的内容。当我单击与数据绑定关联的<td>
时,它会抛出错误"类型错误:h.apply 不是一个函数。(在"h.apply(e,r)"中,"h.apply"未定义)。我在这里做错了什么?
我所说的方法是视图模型原型上的消息方法。
JAVASCRIPT
var message = (function(){
function Message(){
this.main = ko.observable(true);
this.welcome = "Welcome to Tic-Tac-Toe! This is a 2 player game. Click New Game to play!"
this.turn = ", its your turn."
this.win = ", you won!"
this.draw = "It's a draw..."
}
return Message;
})()
var players = (function(){
function Players(){
this.player1 = ko.observable(true);
this.player2 = ko.observable(false);
}
return Players;
})()
var aBox = (function(){
function ABox(){
this.symbol = ko.observable(" ")
}
return ABox;
})()
var viewModel = (function(){
function ViewModel(){
this.GameMessage = new message();
this.thePlayers = new players();
this.r1c1 = new aBox();
this.r1c2 = new aBox();
this.r1c3 = new aBox();
this.r2c1 = new aBox();
this.r2c2 = new aBox();
this.r2c3 = new aBox();
this.r3c1 = new aBox();
this.r3c2 = new aBox();
this.r3c3 = new aBox();
}
/****************************************
************* Messages *****************
****************************************/
ViewModel.prototype.StartMessage = function(){
this.GameMessage.main(false)
}
ViewModel.prototype.Messages = function(){
if(this.GameMessage.main()){
return this.GameMessage.welcome;
}
else if(this.thePlayers.player1()){
this.thePlayers.player1(false);
this.thePlayers.player2(true);
return "Player 1"+this.GameMessage.turn;
}
else if(this.thePlayers.player2())
this.thePlayers.player1(true);
this.thePlayers.player2(false);
return "Player 2"+this.GameMessage.turn;
}
/****************************************
************* GamePlay *****************
****************************************/
/****************************************
************* If needed ****************
****************************************/
return ViewModel;
})()
ko.applyBindings(new viewModel())
HTML(Jade Preprocessor)
p#title.col-xs-12.bg-primary.text-center
| Tic - Tac - Toe!
div.col-xs-3.bg-info
div.bg-primary.controls
span
button.btn.btn-default(data-bind="click:StartMessage.bind($root)")
| New Game
p#message.lead(data-bind="text:Messages.bind($root)()")
table.bg-success(style="table-layout:fixed;")
tr#row1
td(data-bind="click:Messages.bind($root)()")
td  
td  
tr#row2
td  
td  
td  
tr#row3
td  
td  
td  
单击绑定需要函数才能调用。 您似乎正在调用该函数,并绑定到调用该函数的结果。 删除函数调用。
更改此设置:
td(data-bind="click:Messages.bind($root)()")
对此:
td(data-bind="click:Messages.bind($root)")
相关文章:
- Javascript:如何获取函数.apply()的键
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何通过 Function.prototype.apply() 更改调用函数的参数
- 通过fn.apply或fn.bind将函数传递给setTimeout
- 了解窗口调整大小事件与 scope.$apply 与函数$watch
- 节点redis't喜欢函数.apply()
- 未捕获的类型错误:hook.apply不是在react路由器中使用onEnter时的函数
- 函数式编程 - .bind.apply for curry function.
- JS:.apply(null,arguments)——为什么我的函数被调用一次,而console.log被调用多次
- 函数 (){ 返回 fn.apply(me, arguments); } 作为回调
- 将函数绑定到baz后调用fn.apply(bar,[])
- 使用数组-apply()中的参数调用函数,但不使用上下文参数
- 类型错误: $scope.apply 不是一个函数
- $.when.apply.done 不执行异步函数
- node.js 使用 function.prototype.apply 执行的泛型函数
- KNOCKOUT.js问题:“H.apply不是一个函数.(在'h.apply(e,r)'中,'
- 如果不在Angular.js中使用$apply(),我该如何编写此函数
- 为什么在构造函数中需要apply()函数
- Internet Explorer - javascript “apply” 函数在 window.external e
- 实现 Function.prototype.apply 函数