Why is the value not being assigned by the Angular component from the observable service getter?

I am currently working on developing a filter set, and I am facing an issue with the salesChannels array content in my view. The array only gets populated after clicking a button that triggers the test() function. Interestingly, the first time I log the array in ngOnInit, it shows up empty, but it works fine after clicking the button. The getOrganisationChannels function returns an observable.

What is causing this behavior and how can I handle it correctly? I attempted to use an eventEmitter to trigger the population process, but that did not yield the desired result.

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>

Answer №1

It is normal behavior to populate the salesChannel in the subscription of an Observable. It is recommended to utilize the async pipe to allow Angular to monitor 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>

For more information, visit: https://angular.io/api/common/AsyncPipe

Answer №2

An effective solution would be to implement the AsyncPipe in this scenario:

<div>{{ asyncSalesChannels | async}}</div>
and within the TypeScript file:
asyncSalesChannels = this.orgService.fetchOrgChannels(this.orgId)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Preventing multiple tabs in a form with PHP

I have successfully used JavaScript to prevent a link from being opened in multiple browser tabs every time a user clicks on it. For example, if the destination of my link is Google, it will create a new tab if one does not already exist but refresh the ex ...

Express route fails to wait for Redis server response before moving on

How can I modify the code below to ensure that the res.json(...) command is not executed until all open calls to the Redis client.somecommand(..) have completed successfully? An issue occurs in the code where the client.hmset(uname, { ... } attempt to set ...

Execute a separate function when clicking on certain buttons without interfering with the buttons' original onclick function (which has already been assigned) - JavaScript

While I am still relatively new to JavaScript and learning, I have developed a webpage with multiple buttons that trigger different functions on click events. However, I would like to additionally call another function when any of these buttons are clicked ...

Troubleshooting: Issues when using jQuery metisMenu in conjunction with *ngIf

We are facing an issue while using the metisMenu with Angular2. There is an *ngIf condition on one of the list items, and its visibility changes based on whether a record is selected. When the li item is shown for additional options, it does not work prope ...

Creating a custom toJSON function for a property declared using Object.defineProperty

Let's consider a scenario where we have an object with a specific property that is meant to reference another object, as shown below: Object.defineProperty(parent, 'child', { enumerable: true, get: function() { return this._actualCh ...

Transitioning a JavaScriptIonicAngular 1 application to TypescriptIonic 2Angular 2 application

I am currently in the process of transitioning an App from JavaScript\Ionic\Angular1 to Typescript\Ionic2\Angular2 one file at a time. I have extensively researched various guides on migrating between these technologies, completed the A ...

Using the Grails asset-pipeline with an external JavaScript library

In transitioning from Grails 2 to Grails 3, I am facing the challenge of managing my JavaScript files with the asset-pipeline plugin. The issue lies in using external libraries such as globalize and ajax-solr, which are large and consist of multiple interd ...

Limit file upload size to less than 1MB in Angular 2 with typescript using ng2-file-upload

Having issue with my code - I can't upload a file larger than 1mb even though maxFileSize is set to 50mb. Can anyone help me troubleshoot? @Component({ moduleId: module.id, selector: 'NeedAnalysisConsult', templateUrl: 'nee ...

Error alert: Blinking display, do not dismiss

I am trying to make it so that when the "enviar" button is clicked, the "resultado" goes from being hidden ("display:none") to being visible ("display:block"). This is my html document: <script type="text/javascript"> function showResu ...

How can I pass DOCUMENT in Angular?

In my directive, I use dependency injection to access the DOCUMENT and set up an event listener: constructor(@Inject(DOCUMENT) private document: Document) {} ngOnInit() { this.document.addEventListener('click', this.clicked, true); } @Bound ...

Is it possible to alter the state of one page by clicking a link on another page?

Is it possible to update the state of a different page when a link is clicked and the user navigates to that page? For example: Homepage <Link href="/about"> <button>Click here for the About page</button> </Link> If a ...

Calculate the cumulative values in ng-repeat using AngularJS

I used ng-repeat to iterate through a JSON array. I calculated the number of nights by utilizing the dayDiff() function. Now, I need to find the total number of nights for all invoices. My project is built with AngularJS. Is there a way to retrieve the to ...

Mapping DOM elements to VueJS components for hydration is a key process in facilitating

I have a specific requirement and I am exploring potential solutions using VueJS, as it offers the convenient feature of hydrating pre-rendered HTML from the server. In my Vue components, I do not define a template within the .vue file, but I need them to ...

Accessing the current route in a Vuex module using Vue.js

I've created a vuex store with namespaces that retrieves a specific store entry based on the current route parameter. import Router from '../../router/index' const options = { routeIdentifier: 'stepId' } export function fetchFr ...

The Angular Fire Firestore module does not include the 'FirestoreSettingsToken' in its list of exported members

When I initially compiled my project, this issue occurred. The error message displayed is as follows: Module '".../node_modules/@angular/fire/firestore/angular-fire-firestore"' has no exported member 'FirestoreSettingsToken' In my a ...

Having trouble locating the mongoDB module while trying to deploy on Heroku

After deploying my node.js server to Heroku, I encountered the following error message: 2018-12-27T10:10:28.370131+00:00 app[web.1]: Error: Cannot find module './lib/utils' 2018-12-27T10:10:28.370137+00:00 app [web.1]: at Function.Modul ...

What is the best way to halt a CSS transition using JavaScript when dealing with an image?

I am facing an issue while attempting to create a basic CSS transition using JavaScript. The goal is for the start button to trigger the movement of an element, based on the duration of the keyframe animation. Then, clicking the end button should make the ...

Using jQuery to toggle the visibility of HTML elements

Hi there, I am trying to create an interactive HTML sidebar where each section shows its respective posts when clicked. However, I am facing issues as the content keeps hiding continuously. <div class="col-md-3"> <div class="list-grou ...

Understanding the behavior of the enter key in Angular and Material forms

When creating forms in my MEAN application, I include the following code: <form novalidate [formGroup]="thesisForm" enctype="multipart/form-data" (keydown.enter)="$event.preventDefault()" (keydown.shift.enter)="$ev ...

Exploring the MVVM architecture in React and the common warning about a missing dependency in the useEffect hook

I'm currently in the process of developing a React application using a View/ViewModel architecture. In this setup, the viewModel takes on the responsibility of fetching data and providing data along with getter functions to the View. export default f ...