淘汰赛:嵌套的 dependentObservable - 不起作用

发布于 2024-12-11 10:18:07 字数 1680 浏览 0 评论 0原文

我是 Knockout JS 的新手。 我需要按如下方式绑定嵌套数组

名称:下拉

电子邮件:所选用户的姓名

联系方法类型:从ContactInfo中下拉联系方法类型选择

联系人值: 来自 ContactInfo 的实际值

我已获得姓名、电子邮件和联系人值。我需要知道如何在下拉列表中选择联系方法类型值,并且同样需要绑定到联系值。

我收到以下错误 错误:无法获取属性“ContactMethodType”的值:对象为 null 或未定义

function LifelineViewModel() {

    this.lifelines = ko.observableArray([{
        Name: "",
        Email: "",
        ContactInfo:
                {
                    ContactMethodType: "",
                    ContactValue: ""
                }
    }]);
    this.selectedLifeline = ko.observable();

    this.contactTypes = ko.observableArray([{Name: ''}]);

    this.selectedContactInfo = ko.dependentObservable(function () {
        if (this.selectedLifeline() === undefined) return null;
        return this.selectedLifeline().ContactInfo;
    }, this);

    this.selectedContactMethodType = ko.dependentObservable(function () {
        if (this.selectedContactInfo() === undefined) return null;
        return this.selectedContactInfo().ContactMethodType;
    }, this);

}

HTML 代码

<select data-bind="options: lifelines, optionsText: 'Name', value: selectedLifeline"></select>
<p><span data-bind="text: selectedLifeline().Email"></span></p>
<p><span data-bind="text: selectedContactInfo().ContactMethodType + ' ' + selectedContactInfo().ContactValue"></p>
<select data-bind="options: contactTypes, optionsText: 'Name', value: selectedContactMethodType"></select>

I am new to Knockout JS.
I need to bind nested arrays as folows

Name: Drop down

Email: Selected user's name

Contact Method Type: Drop down with Contact Method type from ContactInfo selected

Contact Value: Actual value from ContactInfo

I have got Name, email and contact value working. I need to know how to have contact method type value being selected in the drop down and the same need to be bound to contact value.

I get following error
Error: Unable to get value of the property 'ContactMethodType': object is null or undefined

function LifelineViewModel() {

    this.lifelines = ko.observableArray([{
        Name: "",
        Email: "",
        ContactInfo:
                {
                    ContactMethodType: "",
                    ContactValue: ""
                }
    }]);
    this.selectedLifeline = ko.observable();

    this.contactTypes = ko.observableArray([{Name: ''}]);

    this.selectedContactInfo = ko.dependentObservable(function () {
        if (this.selectedLifeline() === undefined) return null;
        return this.selectedLifeline().ContactInfo;
    }, this);

    this.selectedContactMethodType = ko.dependentObservable(function () {
        if (this.selectedContactInfo() === undefined) return null;
        return this.selectedContactInfo().ContactMethodType;
    }, this);

}

HTML code

<select data-bind="options: lifelines, optionsText: 'Name', value: selectedLifeline"></select>
<p><span data-bind="text: selectedLifeline().Email"></span></p>
<p><span data-bind="text: selectedContactInfo().ContactMethodType + ' ' + selectedContactInfo().ContactValue"></p>
<select data-bind="options: contactTypes, optionsText: 'Name', value: selectedContactMethodType"></select>

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

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

发布评论

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

评论(1

各自安好 2024-12-18 10:18:07

你的问题在于你的第二个依赖Observable。默认情况下,dependentObservables 在创建时第一次被评估。在您的情况下,selectedContactMethodType将从selectedContactInfo获取当前值,该值将为null。这与您的 undefined 检查不匹配,然后尝试从 null 读取 ContactMethodType ,这会导致错误。

因此,您需要更加小心地对待 undefined 和 null。

使用 1.3 beta< 中的控制流绑定/a>,这是您的示例,没有使用 dependentObservables 来防止 null: http://jsfiddle.net/rniemeyer/9msqK/

Your issue lies in your second dependentObservable. By default, dependentObservables are evaluated for the first time when they are created. In your case, selectedContactMethodType will get the current value from selectedContactInfo which will be null. This will not match your undefined check and then try to read ContactMethodType off of null which causes the error.

So, you would need to be a bit more careful in how you treat undefined vs. null.

Using the control-flow bindings in 1.3 beta, here is your sample without using dependentObservables to protect against null: http://jsfiddle.net/rniemeyer/9msqK/

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