我想动态添加属性 id 到标签,但它给出了错误
"Uncaught ReferenceError: Unable to process binding "attr: function (){return {id:id} }" Message: id is not defined"
我的html-
<label data-bind="text:label"></label>
<span class="name" data-bind="attr:{id:id},html:value"></span>
<span class="cancelled" data-bind="html:cancel"></span>
<span class="currency-block" data-bind="html:amount"></span>
我的 View 模型-
this.listrows = [
{layout:'basic', params:{icon:'icon-phone', items:[
{item:'content', type:'basic', params:{id:'test', label:'From Account',value:'asuhdas'}}
]}},
{layout:'basic', params:{icon:'icon-money', items:[
{item:'content', type:'basic', params:{label: 'To Account:', value:''}}
]}}
]
我添加了 attr:{id:id} 仍然给出错误。任何人都可以帮助我吗?
最佳答案
在看不到更多 HTML 内容的情况下(因此假设您的 HTML 包含在针对 listrows.item
数组的 foreach
中),id
属性包含在您的 params
对象中,因此为了引用它,您需要指定 params.id
:
<span class="name" data-bind="attr: {id: params.id }, html: params.value"></span>
<小时/>
编辑:基于评论:
above is my full html. I just want to add id to first span...
在这种情况下,您需要访问 View 模型的值本身。您不能仅调用 id
,因为您的 View 模型没有自己的 id
属性。相反,您需要使用以下方式访问它:
attr: { id: listrows[0].items[0].params.id }
这假设您只想访问 listrows
和 items
数组的第 0th
索引。
关于javascript - knockout 组件绑定(bind)属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25931498/