我正在使用knockout.js和upshot.js来获取服务器上API控制器的项目列表。这基本上是我的视图模型:
self.templatesDataSource = upshot.dataSources.Templates.refresh();
self.templates = self.templatesDataSource.getEntities();
绑定到属性可以正常工作:
<div data-bind="foreach: templates">
<a href="#" data-bind="text: Title"></a><br />
</div>
但是,让我说我希望文本不仅显示标题,还包括标题和其他值的组合?假设我想要Title + ' ' + Id
。根据{{3}}中的“示例4”,我认为我应该能够做到这样的事情:
<div data-bind="foreach: templates">
<a href="#" data-bind="text: function(item){return item.Title + ' ' + item.Id; }"></a><br />
</div>
但是,我看到函数文本(function(item){...
等)而不是结果。我还需要这个功能来为链接构建适当的href。我怎么能做到这一点?
答案 0 :(得分:2)
你可以直接写下你的陈述,如:
data-bind="text: Title + " " + Id"
如果您的属性是可观察的,那么您可以这样做:
data-bind="text: Title() + " " + Id()"
对于href
的绑定,你会这样做:
data-bind="attr: { href: Url }"
计算值时的另一个选项是使用computed observable来表示值。