剔除视图模型属性未定义

发布于 2025-01-05 22:14:35 字数 1948 浏览 0 评论 0原文

我有一个简单的 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 来测试:

http://jsfiddle.net/E7kUr/

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:

http://jsfiddle.net/E7kUr/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自我难过 2025-01-12 22:14:35

因此,您有几个选择:

  1. 当您尝试绑定未定义的属性时,KO 会遇到问题,除非它们脱离对象。因此,您可以使用 $data. 为各种绑定添加前缀,KO 将能够解析您的绑定。示例: http://jsfiddle.net/rniemeyer/dLCL8/ 如果您知道多个属性始终会如果在一起,那么您可以在这些选项周围使用 withif 语句。

  2. 处理“未定义”属性的另一种方法是创建一个绑定,在这些属性丢失时填充这些属性。看看这个答案。它会类似,但可能带有“文本”绑定。示例:http://jsfiddle.net/rniemeyer/dLCL8/4/

So, there are a few options that you have:

  1. 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 a with or if statement around those options.

  2. 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/

灼痛 2025-01-12 22:14:35

使用$data前缀,KO可以解析它

示例:
http://jsfiddle.net/baryon/NsuL7/1/

Use the $data prefix, KO can parse it

Sample:
http://jsfiddle.net/baryon/NsuL7/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文