2016-09-27 120 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(); 
}