剔除视图模型属性未定义
我有一个简单的 foreach:
<div id="customersArea" data-bind="foreach: people">
<div class="section" data-bind="attr: { 'personid': PersonId }" >
<div class="sectionActions">
<div><a class="action" href="#" data-bind='click: $parent.removePerson'>Remove</a></div>
</div>
<div class="sectionText">
<span data-bind="if:LastName, text:LastName"></span>
<span data-bind="if:FirstName, text:FirstName"></span>
<span data-bind="if:MailingAddress">
<span data-bind="with:MailingAddress">
<span data-bind="text:StreetPartOne"> </span>
<span data-bind="text:StreetPartTwo"> </span>
<span data-bind="text:City"></span>
<span data-bind="text:PostalCode"></span>
</span>
</span>
<span data-bind="if:EmailAddress, text:EmailAddress"></span>
<span data-bind="if:MainPhoneNumber, text:MainPhoneNumber"></span>
<span data-bind="if:MobilePhoneNumber, text:MobilePhoneNumber"></span>
</div>
<div class="sectionOptions">
</div>
</div>
</div>
我试图使其可以绑定到模型 {PersonId:33} ,而其余部分如果丢失则不会呈现。当我尝试这种方法和其他方法时,
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: MailingAddress is not defined;
Bindings value: if:MailingAddress
我创建了一个简单的 jsfiddle 来测试:
I have a simple foreach:
<div id="customersArea" data-bind="foreach: people">
<div class="section" data-bind="attr: { 'personid': PersonId }" >
<div class="sectionActions">
<div><a class="action" href="#" data-bind='click: $parent.removePerson'>Remove</a></div>
</div>
<div class="sectionText">
<span data-bind="if:LastName, text:LastName"></span>
<span data-bind="if:FirstName, text:FirstName"></span>
<span data-bind="if:MailingAddress">
<span data-bind="with:MailingAddress">
<span data-bind="text:StreetPartOne"> </span>
<span data-bind="text:StreetPartTwo"> </span>
<span data-bind="text:City"></span>
<span data-bind="text:PostalCode"></span>
</span>
</span>
<span data-bind="if:EmailAddress, text:EmailAddress"></span>
<span data-bind="if:MainPhoneNumber, text:MainPhoneNumber"></span>
<span data-bind="if:MobilePhoneNumber, text:MobilePhoneNumber"></span>
</div>
<div class="sectionOptions">
</div>
</div>
</div>
I am trying to make it such that i can bind against a model {PersonId:33} and the rest will just not render if missing. when i try it this and other ways i get
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: MailingAddress is not defined;
Bindings value: if:MailingAddress
I created a simple jsfiddle to test:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,您有几个选择:
当您尝试绑定未定义的属性时,KO 会遇到问题,除非它们脱离对象。因此,您可以使用
$data.
为各种绑定添加前缀,KO 将能够解析您的绑定。示例: http://jsfiddle.net/rniemeyer/dLCL8/ 如果您知道多个属性始终会如果在一起,那么您可以在这些选项周围使用with
或if
语句。处理“未定义”属性的另一种方法是创建一个绑定,在这些属性丢失时填充这些属性。看看这个答案。它会类似,但可能带有“文本”绑定。示例:http://jsfiddle.net/rniemeyer/dLCL8/4/
So, there are a few options that you have:
KO will have an issue when you try to bind against undefined properties, unless they are off of an object. So, you can prefix your various bindings with
$data.
and KO will be able to parse your bindings. Sample: http://jsfiddle.net/rniemeyer/dLCL8/ If you know that several properties will always be together, then you could use awith
orif
statement around those options.A different take on handling "undefined" properties is to create a binding that populates these properties when they are missing. Look at this answer. It would be similar, but potentially with the 'text' binding. Sample: http://jsfiddle.net/rniemeyer/dLCL8/4/
使用
$data
前缀,KO可以解析它示例:
http://jsfiddle.net/baryon/NsuL7/1/
Use the
$data
prefix, KO can parse itSample:
http://jsfiddle.net/baryon/NsuL7/1/