Home > Back-end >  Changes not reflecting in html when the Observable is subscribed
Changes not reflecting in html when the Observable is subscribed

Time:02-07

I'm trying to pass the object to object Model using a shared service to a already loaded component , even though i get data after subscription, it is not reflecting in html view.

<div *ngFor="let emp of emps" >
      <div >
            <img  [src]="emp?.logo" />
                </div>
      <div >
      <p  >{{emp?.name}}</p>
      <p >Emp Code: {{emp?.code}}</p>
       </div>
                   
       <div >
             <a  >View Details</a>
       </div>
  </div>
emps : EmployeeModel[];


ngOnInit(): void {

    this.service.getData().subscribe(data => {
      this.emps = data
      console.log(this.emps))
    })
   }

export class EmployeeService {
  employeeData:any;
  private subject = new Subject<any>();
  constructor() { }
  
  setEmployeeData(data) {
    this.employeeData = data
    this.subject.next(data);
  }

 getData(): Observable<any> {
    return this.subject.asObservable();
 }
} 

  

CodePudding user response:

Use BehaviorSubject instead of Subject

private subject = new BehaviorSubject<any>(null);

CodePudding user response:

The observable being returned from getData() isn't emitting anything. That is until you invoke setEmployeeData() with data.

  •  Tags:  
  • Related