无法在html中调用.js(使用淘汰赛)

时间:2012-09-17 13:53:55

标签: javascript html knockout.js knockout-2.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());

感谢。

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分: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());