Preserving the value of a function argument for future reference

I have a function called myFunction() that accepts one argument. My goal is to save this argument to a variable and be able to access it later.

Here is what I am attempting to do:

When a user performs an action, an event is passed as an argument to the myFunction. Inside myFunction, I am assigning the event to a variable called saveArg1.

In component x, I am making a change that should trigger a call to myFunction in component 1. This function call is made using subject.

However, the issue arises when calling myFunction as saveArg1 is undefined.

So, how can I store an event for later use without relying on localStorage?

**Component 1**
public saveArg1: any;
ngOnInit(){
 this.myService.isChanged.subscribe((result) => {
      if (result) {
       this.myFunction(this.saveArg1);
      }
    });
}

myFunction(arg1) {
this.saveArg1 = arg1;
....
}

**Component x**
  onWeekChange($event) {
    this.myService.isChanged.next(true);
  }

Answer №1

Ensure that you assign an initial value to your variable saveArg1, even if it is empty.

Within your component 1,

public saveArg1:any="";

Following these steps should help resolve the problem.

Answer №2

It is worth considering adding this.saveArg1 to a related topic as well. This is important because when you invoke this.myService.isChanged from another component, your component is rebuilt from the beginning, which is why saveArg1 loses its assigned value.

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

Encountering an unexpected identifier error in Sails JS

As a newcomer to Sails JS, I am attempting to perform CRUD operations with it. However, I am encountering an unexpected error in my index function. I am struggling to identify where the mistake lies. // Usercontroller.js file module.exports = { creat ...

Delete one item from a group of objects[]

In my TypeScript code, I have a declared object like this: public profileDataSource: { Value: string, Key: number }[]; This results in an object structure that looks similar to the following: 0: Object {Value: "<Select Profile>", Key: null} ...

swap out an element in an array with an extra element

My array contains elements with both id and des properties. I would like to add an additional property like value:0 to each object in the array. I achieved this using a loop. let data = [ { "id": 1001, "des": "aaa" }, { ...

Using Angular's ngForm within an ng-template

Within my ng-template, there is a form displayed in a modal. .ts @ViewChild('newControlForm', {static: false}) public newControlForm: NgForm; .html <ng-template> <form role="form" #newControlForm="ngForm"> </form> </ng ...

Having trouble accessing and establishing a connection with MongoDB

I've been working on a mini URL shortener project using JavaScript, Express, and MongoDB, but I've encountered some errors when trying to launch my local server and connect to MongoDB! Here's a snippet of my code: const express = require(&ap ...

Tips on avoiding quotation marks in a Less variable containing a color identifier

I'm currently working on an HTML/CSS project where I aim to establish classes for labels and texts based on their color. For instance: .text-red{ color: red; } .label-white{ color: white; } In order to achieve this, my approach involves cr ...

Utilizing the .map() method along with bootstrap's <Col sm="4" /> grid system in a React application

Recently, I integrated ReduxLazyScroll for infinite scroll functionality in my app. Previously, it was a simple ul -> li list. Now, I want to display 3 cards side by side in a bootstrap-grid style layout like this: <Row> <Col sm="4"& ...

When provided with varied inputs, new Date() yields distinct values for various time zones

var date1 = "2015-03-29"; console.log(new Date(date1)); //Output:Sun Mar 29 2015 05:30:00 GMT+0530 (India Standard Time) var date2 = "1869-12-31"; console.log(new Date(date2)); //Output:Fri Dec 31 1869 05:53:20 GMT+0553 (India Standard ...

Angular and RxJs come together to create a compelling feature: a dynamic observable that resolves based on user

We are currently developing a messenger module that requires RxJs for emitting a value based on user interaction with another component. After attempting to use of() and passing an existing BehaviorSubject, neither method worked as expected. The desired f ...

Set up a cronjob based on the specified time entered in an HTML form

For my HTML form, users enter a time in HH:mm format. I aim to set up a cronjob on the system that will automatically delete a specific file based on the user-provided time. The file itself always remains constant, with only the deletion time varying. Th ...

Selenium - How to pass a file path to a dynamically generated input element that is not visible in the DOM

Check out this example of HTML code: This is how you create a visible button and display the selected file: <button id="visible-btn">visible button</button> <p>selected file is: <span id="selected-file"></spa ...

Retrieve the data stored in a selection of checkbox fields on a form

I have a table of checkboxes: <div> <h1 class="text-center">Select activities</h1> <div class="row"> <div class="col"></div> <div class="col-md-8 col-lg-8"> <h3>Link activ ...

Encountering issues during the installation of JSNLog

Encountering some challenges with the installation and utilization of jsnlog following the instructions provided on the documentation. Continuously encountering errors when attempting to import or use the library. Attempted importing the library as outlin ...

What is the best way to save emitted values from rxjs?

I'm currently grappling with understanding rxjs. In my angular project, I've created a service that emits and listens to boolean values that indicate the start and completion of asynchronous operations. export class UpdateScriptRunningEventServi ...

Tips for Providing a Generic Type to a Component Imported Using Next.js Dynamic

There is no issue with this code snippet: import DatasheetContainer from '@/uikit/detailed-view/DatasheetContainer'; const DetailedView = () => { return ( <Page> <PageBody direction="row" bgColor="white&qu ...

What is up with this strange JavaScript variable string / DOM selection?

I'm facing a strange issue with a variable that appears to be a string when alerted, but in the console, it is actually a DOMSelection presented in tree form. How can I retrieve the actual string value from this console output? DOMSelection anchorNod ...

The system encountered an error when attempting to convert the data '19-10-2002' into a date format

I am trying to pass a date parameter in the format (dd-MM-yyyy) and then convert it into the format (yyyy-MM-dd) before sending it via API. Here is my code: convert(date:string){ date //is in the format(dd-MM-yyyy) date = formatDate(date , " ...

Sharing markdown content between two Vue.js components

I have a markdown editor in View A which is displaying the result in the current View. My goal is to share this result with another page, View B. In View A, there is a button that allows the user to share the markdown result with View B. I am using a texta ...

Most efficient method for dynamically changing HTML content with JavaScript

Can anyone provide guidance on how to switch HTML content using JavaScript? For example: if($sid == 0) {insert one HTML code} else {insert another HTML code} Which method is preferable - LESS, jQuery, CSS3, etc? ...

Using Vue.js, you can bind a JSON object as the value of a checkbox input to the

Can a Json Object be passed as a value on a checkbox? I have multiple checkboxes as shown below... selectedUsers is an array containing the selected values... I would like to end up with a json array like [{"userid": "12345"}, {"userid": "54321"}] <inp ...