【问题标题】:Can not call .js in html(using knockout)无法在 html 中调用 .js(使用淘汰赛)
【发布时间】:2012-09-17 13:53:55
【问题描述】:

我是使用 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());

谢谢。

【问题讨论】:

  • 我的意思不是要让人觉得很苛刻,但你的问题很令人困惑..你能提供一些更相关的代码吗?

标签: javascript html knockout.js knockout-2.0


【解决方案1】:

您不是在创建 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());

【讨论】:

    【解决方案2】:

    此代码必须出现在绑定的 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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-09
      • 2018-03-31
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2013-04-28
      相关资源
      最近更新 更多