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

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

问题描述

我需要在一个元素上绑定多个数据.例如,我想要一个 href 以及 html 数据绑定在 one a 标签上.我试过了,

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>

但这行不通.似乎knockout 只支持绑定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天全站免登陆