将2个可观察量合并/合并为一个

时间:2017-04-25 09:34:41

标签: javascript angular reactjs typescript observable

我试图结合2个Observables。第一个observable持有protected void SearchByTagButton_Click(object sender, EventArgs e) { String strConn = "server = localhost; user id = root; pwd = ...; persistsecurityinfo = True; database = knowledgebase"; MySqlConnection conn = new MySqlConnection(strConn); conn.Open(); MySqlCommand cmd = new MySqlCommand("Select * FROM tblafspraak WHERE AfspraakNaam=@SearchByTagTB", conn); try { MySqlParameter search = new MySqlParameter(); search.ParameterName = "@SearchByTagTB"; search.Value = SearchByTagTB.Text.Trim(); cmd.Parameters.Add(search); MySqlDataReader dr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(dr); gvPatients.DataSource = dt; gvPatients.DataBind(); } catch (Exception ex) { Response.Write(ex.Message); } finally { //Connection Object Closed conn.Close(); } } 类,第二个持有ShoppingCart列表。我想将observable与购物车项目ShoppingItems)映射到名为Observable<ShoppingItems的{​​{1}}类中的实例变量。

可观察1: ShoppingCart

这是购物车类的片段。

shoppingList$

可观察2: Observable<ShoppingCart>     private observable:Observable

这是我正在使用的功能。

export class ShoppingCart {
private shoppingList$: Observable<ShoppingItems> 
private selectedIndex: number;
... etc
}

然而,上述方法会返回Observable<ShoppingItems[]>而不是retrieveShoppingCartComplete(shoppingCartId: string): Observable<ShoppingCart> { /**Returns the requested shopping cart, with the Ids of the shopping items in an array called shoppingCart.ShoppingItems*/ let shoppingCart$ = this.retrieveShoppingCart_AsEntity(shoppingCartId); /** returns an Observable<ShoppingItems[]> of the requested ShoppingItems as entitys*/ let shoppingItems$ = shoppingCart$.map(shoppingCart => this.leS.retrieveShoppingItemsComplete(shoppingCart.ShoppingItemsIds)) return shoppingCart$.combineAll(shoppingItems$); } ,其中Observable<[ShoppingCart | ShoppingItems[]]>数组已分配给Observable<ShoppingCart>类中的Observable<ShoppingItems>实例变量

1 个答案:

答案 0 :(得分:0)

我认为combineAll不是你需要的。我会使用flatMap链接两个服务调用,如下所示:

retrieveShoppingCartComplete(shoppingCartId: string): Observable<ShoppingCart> {
    return this.retrieveShoppingCart_AsEntity(shoppingCartId)
        .flatMap(shoppingCart => {
            shoppingCart.shoppingList$ = this.leS.retrieveShoppingItemsComplete(shoppingCart.ShoppingItemsIds)
        })
}