如何在一个元素上具有多个数据绑定属性? [英] How to have multiple data-bind attributes on one element?

查看:106
本文介绍了如何在一个元素上具有多个数据绑定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个元素上具有多个数据绑定.例如,我想要在 one a标记上的href以及html数据绑定.我已经尝试过了,

I need to have multiple data bindings on one element. For example, I want a href as well as a html data-binding on one a tag. I have tried this,

<a data-bind="html: name" 
   data-bind="attr: { href: url }" 
   data-bind="attr: { 'data-prop': xyz }">
</a>

但这不起作用.敲除似乎仅支持绑定 one data-bind属性?如何在一个元素上同时绑定href,内部html和自定义"data-prop"属性?

But this doesn't work. It seems knockout only supports binding one data-bind property? How to bind both the href, the inner html, and a custom "data-prop" attribute on one element?

推荐答案

像这样:

<a data-bind="html: name, attr: { href: url }">

您使用逗号分隔的绑定-属性与传递对象相同:

You use comma-separated bindings - the attribute is the same as passing an object:

{
    html: name, 
    attr: { href: url }
}

或者,如果您要一次询问多个attr绑定:

Or, if you're asking about multiple attr bindings at once:

<a data-bind="html: name, attr: { href: url, 'data-prop': FullName }">

这篇关于如何在一个元素上具有多个数据绑定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆