Knockout js:模板化绑定中的参数
我正在尝试对我的项目中经常使用的一些组件进行模板化。省略 html 内容中引入的冗余。但是我不知道是否可以这样做。
我有一个像这样的模板:
<script type="text/x-jquery-tmpl" id="somefieldtemplate" >
<input name=" Prefix" type="text" data-bind="value: ${ $item.fieldName}SomeField" />
..... mor contents ...
</script>
绑定到输入组件的参数应该是可调整的,正如有人可能通过模板选项看到的那样,所以我有一个像
<div data-bind="template: { name: 'somefieldtemplate',
templateOptions:
{ fieldName:'somefield', displayName:'somefieldlabel' } }">
控制台日志中的错误消息这样的条目:
SyntaxError: Unexpected token {
我将问题缩小到这样一个事实:如果我删除$item.fieldName
及其有效的值。
有人可能对这个问题有启发性的解决方案吗?
编辑:
作为信息,我目前使用的knockout.js版本为:knockout-latest
Im trying to template some components often used in my project. To omit the introduced redundancy in html content. However i didnt figure it out if it is even possible to do so.
I have a template like:
<script type="text/x-jquery-tmpl" id="somefieldtemplate" >
<input name=" Prefix" type="text" data-bind="value: ${ $item.fieldName}SomeField" />
..... mor contents ...
</script>
The parameter bound to the input component shall be adjustable as someone may see via template options so i have an entry like
<div data-bind="template: { name: 'somefieldtemplate',
templateOptions:
{ fieldName:'somefield', displayName:'somefieldlabel' } }">
The error message in my console log is:
SyntaxError: Unexpected token {
I narrowed the problem down to the fact that if i remove $item.fieldName
with its value it works.
Has anybody an enlightening solution to this problem maybe ?
Edit:
As information im currently using knockout.js in version: knockout-latest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,经过一番努力后,我通过转向淘汰赛 1.3 beta(现在是 RC)解决了这个问题,它不使用外部模板引擎。它也不支持
templateOptions
但这不是问题。我刚刚为模板构建了一个自定义数据,其中包含作为属性的主数据以及包含我在 1.2 中在 templateOptions 中传递的内容的其他属性。我将其传递到data
参数中,一切正常。I had the same problem and after struggling a lot, I solved it by moving to knockout 1.3 beta (now it's in RC) which doesn't use external templating engines. It doesn't support
templateOptions
either but that's not a problem. I just constructed a custom data for the template which contains the main data as a property plus other properties that contain the stuff I would have passed in templateOptions in 1.2. I passed it in thedata
parameter, and everything worked fine.尝试
Try
可悲的是我需要自己回答。但我设法让它发挥作用。感谢 RP Niemeyer 的提示。
对于对此感兴趣的每个人:
Observables 是通过注册组件方法在模型
someModel
中动态生成的。此可观察量始终具有类似SomeField
的名称Template:
Template-Binding:
Sadly i need to answer it myself. But i managed to get it working. Thanks to the tip of RP Niemeyer.
For everyone interested in this:
Observables are generated dynamically in the model
someModel
with a register component method. This observables have always names like<field>SomeField
Template:
Template-Binding: