Make sure to execute observables just once, and afterwards verify if they have finished - angular2 typescript

Completely new to the world of Observables, so please forgive me if this is a simple question. I've done some research online, but I can't seem to find the answer I'm looking for.

Take a look at this basic example on Plunker:
https://plnkr.co/edit/kgECPQyoKqY7RamebUUu?p=preview

I'm wondering why my Initialized method isn't being called until my service triggers it. I assumed that the initialized observable would kick in as soon as I use Observable.forkJoin. Do I need to initiate the process somehow?

Also, I can't figure out why the subscribe method in my myService.loadOtherData function doesn't seem to be getting called. Any insights into this would be greatly appreciated.

Thank you in advance!

Answer №1

If you are new to Observables, it is recommended to begin with a simple example.

Here is an example in pseudo-code:

let observable = new Observable<string>(observer => {
  if (1 == 1) {
    observer.next("one equals one");
  } else {
    observer.error("one does not equal one");
  }
});
observable.subscribe(
  response => console.log(response), 
  error -> console.log(error)
);

You can also use a Subject which allows for more flexibility:

let subject = new Subject();
subject.subscribe(response -> console.log(response));
subject.next('hello');

I noticed that you were combining subscribing to the subject and retrieving data with an observable. Do these examples provide enough information or do you require a more specific demonstration?

edit:

Your this.initialized.subscribe 'initiates the process'. You are calling the loadOtherData function too late, causing you to miss the subscription to this.initialized when the data is returned. This is why you do not see console.log('initialized')

edit2:

Keep in mind that a BehaviorSubject retains its data and provides it upon subscription. You do not need to initialize it with an empty string.

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

Adding JSON data to an array for Flot Diagram

I've been struggling with the task of organizing data into an array. The existing temp array is structured as follows: tmp[0][0] = "NY" tmp[0][1] = "52" tmp[1][0] = "FL" tmp[1][1] = "25" My goal is to transfer this data into a new array called data. ...

Retain the input values even after the page is refreshed

I have developed a simple form validation page in ReactJS. The input fields are validated using regex patterns. One issue I encountered is that if I enter data in the input fields and refresh the page before completing the form, the input fields are clear ...

Is it possible to identify if an array is a polygon or multipolygon by examining its GeoJson data?

Recently, I came across an example illustrating a simple polygon. However, I wanted to display countries with complex polygons (multipolygons for some countries). Let me demonstrate the process: Example: "type": "Feature", "properties": { "Na ...

A guide on utilizing server-based data grouping and showcasing it in ag-grid within an angular environment

Is there a way to organize the data based on the server name, which is identified by the object key server? The code snippet below illustrates the structure of the data: rowData = [ { server: "Server 1", ping: "10 ms", ...

Displaying local PDF files with Vue.js - A comprehensive guide

I've been experimenting with different PDF viewers like pdf.js and vue-pdf, but I'm encountering some issues. With vue-pdf, I am able to make it work, but I can't seem to render local files. And when trying out pdf.js, I attempted to downlo ...

Efficiently managing AJAX requests and handling form submissions

Currently, I am in the process of developing a jQuery validation system (which is not officially a plugin yet). This system utilizes my Zend_Form validators to check fields client-side. The advantage is that I only need to define my constraints once instea ...

Dreamweaver restricts my ability to utilize JavaScript for coding

My JavaScript isn't working in Dreamweaver. I have linked the script file correctly using the given code: <script src="file:///C:/Users/Matthew/Desktop/Untitled-2.js" type="text/script"></script> However, when I try to call it with scrip ...

How can you incorporate AJAX to add a paragraph element to your HTML document?

I have encountered an issue with two files in my project. The first file is an HTML document, while the second file is a PHP file called ajax_access_database.php. Originally, I intended for the PHP file to access a database, but complications arose, leadin ...

Manipulating visibility of an input tag using JQuery

Having a simple input tag: <input id="DAhour" type="number" style="width:50px; font-size: xx-small; visibility:hidden"> Initially, the input tag is set to be hidden. Upon changing a combobox to a specific index, it should become visible. Despite su ...

Form validation has not passed the necessary checks

Struggling to create a new user POST form with Bootstrap form validation but encountering an issue. The button code is as follows: <button class="btn btn-primary btn-addUser" type="submit">Add User</button> This button trig ...

Testing React Hook Form always returns false for the "isValid" property

When creating a registration modal screen, I encountered an issue with the isValid value when submitting the form. In my local environment (launched by npm start), the isValid value functions correctly without any issues. However, during unit testing us ...

Tips on applying border to unique marker icon in Angular2 using agm-marker

https://i.sstatic.net/HH36B.png Hello everyone, I've been working on a project and need some assistance. My goal is to dynamically add custom marker icons to specific lat/lng coordinates, each with additional values such as a "color name" for border ...

'Unlawful invocation' problem occurs while attempting to upload a file using ajax

Having trouble with uploading a file using ajax as I keep running into an 'Illegal invocation' error when trying to pass the file. All other input fields are successfully passed (if I exclude the file). Here is a simplified version of my code: ...

Utilizing a single code base for both Web development with Angular 4 and Mobile app development with Ionic 3

I'm interested in finding out if I can utilize the same code base for both my Angular 4 web application and an Ionic 3 mobile application that I need to develop. As someone who is new to Ionic 3, I've been exploring the documentation and discove ...

How can I encode and decode a base64 string using AngularJS1 and TypeScript?

I am currently working with Angular1 using TypeScript and I have a question that needs some clarification. Within the environment that I am operating in, is there a method available to encode and decode a string in base64? Despite conducting extensive re ...

Tips for utilizing Jquery to manage a dropdown designed in button form

<div class="btn-group btn-grp-uk col-xs-12 "> <button id="colorList" type="button" class="btn-phn btn btn-dropdown-white- uk dropdown-toggle col-xs-12" data-toggle="dropdown">Red </button> <ul id="colordrop" class="dr ...

Append a constant string to the conclusion of the route parameter

Using React Router v6.4.1, I am aiming for a consistent conclusion to a series of dynamic routes. Specifically, I want my product detail routes to always end with "-product". For example, if the path is "/shaver-900-product", it should activate my Produc ...

Tips for accessing the post body within an express route

Currently, I am in the process of learning how to construct a web application through the utilization of express, node, and angular. As part of this journey, I have encountered a situation where I am capable of transmitting a post request from angular to t ...

Keep the webpage current without the need to refresh it with a button click

I've been pondering if using .ready would be the right approach to continuously update a webpage. Imagine a scenario where I have a page that tracks the number of a specific tag and displays it on the webpage. The count can be modified with basic Jav ...

Mastering the use of gl Scissor is essential for effectively implementing popups in your

I am attempting to implement a pop-up view on top of the current webGL view. My strategy is as follows: Whenever I need to display a popup, I create a scissorRect and begin rendering the popup scene onto it. I was hoping that the content of the previous s ...