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

我需要在一个元素上有多个数据绑定。例如,我希望 href以及 a标记上的 html数据绑定。我试过了,

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

但这不管用。似乎敲除只支持绑定 data-bind属性?如何将 href、内部 html和定制的“ data-prop”属性绑定到一个元素上?

72916 次浏览

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:

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

you can use like below

data-bind="text: method.method_title, attr: {'id': 'label_method_' + method.method_code}"