简体   繁体   English

如何在一个元素上拥有多个数据绑定属性?

[英]How to have multiple data-bind attributes on one element?

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.例如,我要一个href以及一个html数据绑定在一个a标记。 I have tried this,我试过这个,

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

But this doesn't work.但这不起作用。 It seems knockout only supports binding one data-bind property?似乎淘汰赛只支持绑定一个data-bind属性? How to bind both the href , the inner html , and a custom " data-prop " attribute on one element?如何在一个元素上同时绑定href 、内部html和自定义“ data-prop ”属性?

Like this:像这样:

<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 }
}

Or, if you're asking about multiple attr bindings at once:或者,如果您一次询问多个attr绑定:

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

This is how I implemented the source attribute and click event using data-bind.这就是我使用数据绑定实现源属性和单击事件的方式。 You may find it useful.你可能会发现它很有用。

<img data-bind="{click: function(data, event) {ESVendorWidget.loadFunction(data,event)},
                 attr: {src: $data.Photo.PhotoUrl }}"
     alt="package pic" class="big" />

I simply use:我只是使用:

<input type="checkbox"
    data-bind="click: callFunction(), checkedValue: 0, checked: Card.Days">

for a checkbox element.对于复选框元素。

you can use multiple properties using , like below您可以使用多个属性,如下所示

<a data-bind="attr: { href: url, id: id , class: classvalue}">

object like this像这样的对象

{ url: 'http://stackoverflow.com', id:'newid' , classvalue: 'classname' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM