简体   繁体   English

数据绑定文本与属性名称中的破折号

[英]data-bind text with dash in property name

How do I access a property in a binding with an invalid variable name. 如何使用无效的变量名访问绑定中的属性。 For example: 例如:

// {"customer-name": "Explosion Pills"}
<span data-bind="text: customer-name">

This will try to subtract name from customer . 这将尝试从customer减去name

The following does not work: 以下不起作用:

<span data-bind="text: 'customer-name'">

as it will literally print out "customer-name." 因为它会打印出“客户名称”。 How can I access this property in a binding? 如何在绑定中访问此属性?

You can use the following format: 您可以使用以下格式:

<span data-bind="text: $data['customer-name']"></span>

Here is working fiddle: http://jsfiddle.net/kqntA/ 这是工作小提琴: http//jsfiddle.net/kqntA/

This works : 这有效:

<span data-bind="text: $data['customer-name']">

See fiddle 看小提琴

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

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