2016-09-27 62 views
1

修訂問題的對象數據:如何更新「Observable.of(對象)」

好,調試運行我注意到,該值僅更新在這裏:

this._homeService.getClientInfo(this.user.customer_uuid).subscribe(
    (res) => {this._customerService.setCustomer(this.getCustomerFromResponse(res)).subscribe()}, 
    (err) => {console.error(err)} 
); 

功能getCustomerFromResponse(響應)返回一個客戶對象,我也可以在那裏做一個新的客戶或任何我想要的值傳遞。但是在我的舊帖子(後面)的例子中調用函數setCustomer(customer)並沒有被傳遞給observable的值。爲什麼?

OLD POST: 我正在使用observables,但我並沒有將它們插入。我現在面臨這個問題,但我經常遇到問題。我有這樣的服務:

import {Injectable} from "@angular/core"; 
import {Customer} from "./customer"; 
import {Observable} from "rxjs/Rx"; 
import "rxjs/add/operator/map"; 
import 'rxjs/add/observable/of'; 
import 'rxjs/add/operator/do'; 

@Injectable() 
export class CustomerService { 
    private customer: Customer; 
    private customer$: Observable<Customer>; 

    constructor() { 
    this.customer = new Customer; 
    this.customer$ = Observable.of(this.customer); 
    } 
    setCustomer(customer: Customer) { 
    return this.customer$.do(val => this.customer = val); 
    } 

    getCustomer() { 
    return this.customer; 
    } 

    getObservable() { 
    return this.customer$; 
    } 

} 

我打電話從類app.component getObservable()以更新值eachtime:

this._customerService.getObservable().subscribe(
      res => {this.customer = res}) 

我現在發帖每個類:

客戶.component

import { Component, OnInit } from '@angular/core'; 
import { Router, ActivatedRoute } from '@angular/router'; 
import { Response } from "@angular/http"; 
import { User } from '../../model/user'; 
import { Customer } from '../../model/customer'; 
import { UserService } from "../../model/user.service"; 
import { CustomerService } from "../../model/customer.service"; 
import { ClientsService } from './clients.service'; 
import { HttpWrapperService } from "../../shared/httpwrapper.service"; 
import { NotificationsService } from '../../shared/notifications.service'; 
import { Notification } from '../../shared/notifications.model'; 

的方法:

customerSelect(customer: Customer) { 
    let user = this._userService.getUser(); 
    user.customer_uuid = customer.uuid; 
    this._customerService.setCustomer(customer).subscribe(); 
    this._userService.setUser(user).subscribe(); 
    this.router.navigate(['/home']); 
    } 

login.component

import {Component, Input, OnInit} from '@angular/core'; 
import { Router, ActivatedRoute } from '@angular/router'; 
import { Subscription }   from 'rxjs/Subscription'; 
import { User } from '../../model/user'; 
import {UserService} from "../../model/user.service"; 
import { Customer } from '../../model/customer'; 
import {CustomerService} from "../../model/customer.service"; 
import {LoginService} from "./login.Service"; 
import {HttpWrapperService} from "../../shared/httpwrapper.service"; 
import {AuthService} from "../../shared/auth.service"; 
import { NotificationsService } from '../../shared/notifications.service'; 
import { Notification } from '../../shared/notifications.model'; 

的方法:

ngOnInit() { 
    this.sub = this.route.params.subscribe(params => { 
     if (params['flag'] == 0) { 
      let nullcustomer = new Customer(); 
      nullcustomer.name = ""; 
      this._customerService.setCustomer(nullcustomer).subscribe(); 
      this._authService.logout(); 
     } 
    }); 
} 
ngOnDestroy() { 
    this.sub.unsubscribe(); 
} 

FYI我使用角芯2.0.0穩定釋放。由於我總是面臨着可觀察問題,所以如果有人能夠爲angular2的觀察者提供一個很好的鏈接教程,我會很感激。我搜索了很多,但我找不到足夠好的一個。

回答

1

我找到了解決方案,但我無法理解,如果有人能解釋我爲什麼它會更好。

首先,我必須獲得當前客戶,並且我無法創建新客戶並清除所有屬性。因此,在兩個地方(家庭和客戶),功能將是:

formatCustomer(customerNew: Customer) { 
     let customer = this._customerService.getCustomer(); 
     if (customer == null) { 
      customer = new Customer; 
     } 

     customer.prop1= customerNew.prop1; 
     customer.prop2= customerNew.prop2; 
     ... 
     this._customerService.setCustomer(customer).subscribe(); 
}