BLU
BLU

Reputation: 61

Changes not reflecting in html when the Observable is subscribed

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" class="card">
      <div class="card-body">
            <img class="img-icon" [src]="emp?.logo" />
                </div>
      <div class="icon-text">
      <p class="maintext" >{{emp?.name}}</p>
      <p class="subtext">Emp Code: {{emp?.code}}</p>
       </div>
                   
       <div class="icon-text">
             <a class="view-btn" >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();
 }
} 

  

Upvotes: 0

Views: 330

Answers (2)

zainhassan
zainhassan

Reputation: 1780

Use BehaviorSubject instead of Subject

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

Upvotes: 2

Joshua McCarthy
Joshua McCarthy

Reputation: 1842

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

Upvotes: 0

Related Questions