0

我是使用 knockout2.1.0 进行淘汰赛的新手。我有一个外部 java 脚本文件,但在我的 html 文件中没有调用它。我无法理解。

我在我的 html 文件中添加了以下内容

  <script src="Scripts/TestJavascript.js"></script>

JS文件

///<reference path="~/Scripts/jquery-1.8.1.min.js">
///<reference path="~/Scripts/knockout-2.1.0.debug.js">
$(function AppViewModel() {
this.firstName = ko.observable("rash");
this.lastName = ko.observable("Bertington");
this.fullName = ko.computed(function(){
    return this.firstName() + " " + this.lastName();
}, this);
})
ko.applyBindings(new AppViewModel());

谢谢。

4

2 回答 2

5

您没有创建 ViewModel。您将其传递给 jquery。

尝试

var AppViewModel = function() {
  this.firstName = ko.observable("rash");
  this.lastName = ko.observable("Bertington");
  this.fullName = ko.computed(function(){
      return this.firstName() + " " + this.lastName();
  }, this);
})
ko.applyBindings(new AppViewModel());
于 2012-09-17T13:56:34.690 回答
1

此代码必须出现在绑定的 html 之后或文档就绪事件 (jquery) 中

function AppViewModel() {
    this.firstName = ko.observable("rash");
    this.lastName = ko.observable("Bertington");
    this.fullName = ko.computed(function(){
        return this.firstName() + " " + this.lastName();
    }, this);
};
ko.applyBindings(new AppViewModel());
于 2012-09-17T13:57:18.013 回答