本文介绍了请帮助我理解为什么此计算的可观察值无法访问可观察值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这是小提琴- http://jsfiddle.net/iRamesh/36N4m/

不确定为什么可计算的observable不返回任何值.我知道如何使其正常工作,但不确定为什么当前代码无法正常工作.请帮助

Not sure why computed observable is not returning any value. I know how to make it working but not sure why the current code is not working. please help

推荐答案

计算的可观察对象在创建时立即进行评估.就您而言,尚未创建viewModel,因此会导致错误.

computed observables are evaluated immediately on creation. In your case, viewModel has not been created yet, so this causes an error.

几个备选方案:

-在初始对象文字之外创建它:

-Create it outside of your initial object literal:

var viewModel = {
    firstName: ko.observable("r"),
    lastName: ko.observable("j"),
 };
viewModel.fullName = ko.computed(function() {
        return viewModel.firstName();
});

-在函数中创建视图模型:

-Create your view model in a function:

var ViewModel = function() {
    this.firstName = ko.observable("r");
    this.lastName = ko.observable("j");
    this.fullName = ko.computed(function() {
            return this.firstName();
    }, this);
};

ko.applyBindings(new ViewModel());

这篇关于请帮助我理解为什么此计算的可观察值无法访问可观察值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 10:27