如何在淘汰赛中创建可计算的可观察数组 [英] How to create a computed observable array in Knockout

查看:80
本文介绍了如何在淘汰赛中创建可计算的可观察数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建一个可计算的可观察数组.

I would like to know how to create a computed observable array.

在我的视图模型中,我有2个可观察数组,我想有一个将两个数组组合在一起的计算可观察数组.

In my view model, I have 2 observable arrays, and I would like to have a computed observable array that is simply both arrays combined.

function ViewModel() {
    var self = this;
    self.listA= ko.observableArray([]);
    self.listB = ko.observableArray([]);
    self.masterList= //combine both list A and B

推荐答案

这将合并两个数组并返回合并后的列表.但是,它不是一个可计算的可观察数组(甚至不知道这是否可能),而是一个规则的可计算的可观察数组.

This will combine the two arrays and return the combined list. However, it is not a computed observable array (don't know if that is even possible), but a regular computed observable.

self.masterList = ko.computed(function() {
    return this.listA().concat(this.listB());
}, this);

这篇关于如何在淘汰赛中创建可计算的可观察数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆