【问题标题】:I can't find the right data-bind for my knockout.js code我找不到适合我的 knockout.js 代码的数据绑定
【发布时间】:2019-01-10 22:10:23
【问题描述】:

这是我的 html

<input id="places-search" data-bind="textInput: str, event: {input: filter}" type="text" placeholder="Ex: Loft">
          <div id="list-result">
            <ul class="navbar-nav" data-bind="foreach: res">
              <li class="nav-item">
                <button class="nav-link btn btn-link" data-bind="attr: {title: elem}" onclick="popLWInfo(id)"></button>

还有我的 javascript

<script>
var filterer = function(){
  var self = this;
  self.str = ko.observable("");
  var location = function(data){
    this.id = ko.observable(data.id);
    this.marker = ko.observable(data.marker);
  };
  self.res = ko.observableArray();
  for(var i = 0; i < markers.length; i++){
    self.res.push(new location({id: i , marker: markers[i]}));
  }
}
</script>

我尝试对“foreach: res”使用不同的词 我尝试添加 'data-bind="with: filterer"' 但数组不会显示 我还是不知道自己做错了什么

【问题讨论】:

  • 有什么问题?你的foreach 不工作了?
  • 目前还不是很清楚确切的问题是什么。如果您有一些工作示例/sn-p,那也会容易得多。
  • 请编辑您的问题以准确解释您要做什么。

标签: javascript html data-binding knockout.js


【解决方案1】:

没有示例,很难准确地确定问题所在。如果您可以粘贴整个示例或创建一个 JSFiddle,那就太好了。

虽然它可能已从您的原始帖子中省略,但我看不到任何对 ko.applyBindings 的调用,它可以处理您的视图模型并将其应用于 HTML。

// Insert this code after your function definition
(function(){
    var model = new filterer();
    ko.applyBindings(model);
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2018-04-14
    • 2016-08-01
    • 2012-06-16
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多