Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

373
Views
Angular component doesn't assign value from observable service getter, why?

I'm working on building a set of filters, so I'm just trying to make use of the salesChannels array content in my view, which only gets populated when clicking the button with the test() function. The log in ngOnInit outputs an empty array the first time, but works correctly after pressing the button. The getOrganisationChannels returns an observable.

What causes this behavior and how do I handle it properly? I tried using an eventEmitter to try and trigger the populating but that doesn't work.

TYPESCRIPT

export class SalesChannelFilterComponent implements OnInit {

    constructor(
        public organizationService: OrganizationService
    ) { }

    @Input() organizationId: any;

    salesChannels: Array<any> = [];
    selectedChannels: Array<any> = [];
    allSelected: Array<any> = [];

    ngOnInit() {
        this.getChannels();
        console.log(this.salesChannels);
    }

    getChannels() {
        this.organizationService.getOrganizationChannels(this.organizationId).subscribe(
            salesChannels => {
                this.salesChannels = salesChannels;
            })
    }

    test() {
        console.log(this.salesChannels);
    }
}

HTML

<div>
    {{ salesChannels | json }}
</div>

<button (click)="test()">test</button>

<div *ngFor="let channel of salesChannels; let i = index;" class="checkbox c-checkbox">
    <label>
        <input type="checkbox">
        <span class="fa fa-check"></span>{{channel.name}}
    </label>
</div>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is expected behaviour since you are populating the salesChannel in the subscription of an Observable. It's recommended that you use aysnc pipe to let angular check for changes and update the view accordingly.

Component.ts :

export class SalesChannelFilterComponent implements OnInit {

    constructor(
        public organizationService: OrganizationService
    ) { }

    @Input() organizationId: any;

    salesChannels$!: Observable<Array<any>>;
    selectedChannels: Array<any> = [];
    allSelected: Array<any> = [];

    ngOnInit() {
        this.getChannels();
        console.log(this.salesChannels);
    }

    getChannels() {
      this.salesChannels$ = this.this.organizationService.getOrganizationChannels(this.organizationId);

    }

    test() {
        console.log(this.salesChannels);
    }
}

In your template:

<button (click)="test()">test</button>

<div *ngFor="let channel of salesChannels$ | async; let i = index;" class="checkbox c-checkbox">
    <label>
        <input type="checkbox">
        <span class="fa fa-check"></span>{{channel.name}}
    </label>
</div>

More details: https://angular.io/api/common/AsyncPipe

about 4 years ago · Juan Pablo Isaza Report

0

I recommend using AsyncPipe here: <div>{{ salesChannels | async}}</div> and in .ts: salesChannels = this.organizationService.getOrganizationChannels(this.organizationId)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!