【问题标题】:knockout data-bind with a function call用函数调用敲除数据绑定
【发布时间】:2018-05-07 16:53:31
【问题描述】:

我正在尝试在数据绑定中调用一个函数,但似乎没有任何效果。

这是我的场景:

function ViewModel() {

  this.Products = ko.observableArray([]);

  this.FormatUrl = function (url) {
     ...
     return ...
  }
}

用法:

<tbody data-bind="foreach: Products">

 ...
    <td><div data-bind="attr:{style: images.length > 0 ? 'background-image:url($root.FormatUrl(images[0]));' : ''}" /></td>

 ...
</tbody>

我还尝试了引用 '$root.Format..' 并得到了 SyntaxError: Unable to parse bindings. Message: Unexpected identifier

调用我的函数的正确方法是什么?

【问题讨论】:

    标签: knockout.js


    【解决方案1】:

    这里的问题是您将字符串与函数调用混合在一起:

    'background-image:url($root.FormatUrl(images[0]));'
    

    你要么需要这样构造它:

    'background-image:url(' + $root.FormatUrl(images[0]) + ');'
    

    或者也许从您的 FormatUrl 函数返回整个字符串,而不是在您的视图中连接。

    【讨论】:

      猜你喜欢
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多