2017-02-09 4 views
0

두 자식 구성 요소간에 정보 공유를위한 앵귤러 2 서비스가 있는데 두 구성 요소가 서비스와 잘 통신하고 있지만 서비스에 변수 값을 설정할 수 없습니다. 다음은 서비스의 코드입니다 :각도 2의 서비스 변수

import { Injectable } from '@angular/core'; 
import { Http, Headers, Response } from "@angular/http"; 
import { Observable } from "rxjs"; 
import { Subject } from 'rxjs/Subject'; 
import {BehaviorSubject} from 'rxjs/Rx'; 
import 'rxjs/Rx'; 

@Injectable() 
export class ServiceNotifications { 

    newPacient: boolean; 

    nuevoPacient$: Subject<boolean> = new BehaviorSubject<boolean>(this.newPacient); 

    constructor() { 
     this.newPacient$.asObservable(); 
    } 

    notiNewPacient(status:boolean){ 
     console.log(status); 
     this.newPacient=status; 
     this.newPaciet$.next(this.newPacient); 
    } 
} 

내 문제는 내가 함수 notiNewPacient와 변수 newPacient를 업데이트 할 수 있다는 것입니다().

+0

'newPaciet $'는 어디에도 정의되어 있지 않지만 어쩌면 오타입니다. 변수를 업데이트 할 수 없다는 것은 무엇을 의미합니까? 또한 생성자가 아무 것도하지 않습니다. – rob

+0

pacient가 만들어 질 때마다 "newPacient"를 "true"값으로 업데이트하려고합니다. 나는 "notiNewPacient()"로 그것을하려고 노력하고있다. 그러나 나는 그것을 얻을 수 없다. 서비스가 잘 작동하고 있습니다. 구성 요소는 Observable에 연결됩니다. – Osmon

답변

0

관찰 할 수없이 시도하십시오. 서비스에서

:

constructor() { 
    this.newPacient$ = true; 
} 

구성 요소에서 : 당신이 구성 요소에서 newPacient에 액세스하려면

constructor(service: serviceNotifications) { 
} 

get newPacient(): boolean { 
    return service.newPacient; 
} 

그런 다음 this.newPacient으로 수행 할 수 있습니다. 그리고보기에서 단지 newPacient.