Tips on transferring component changes from one page to another

I have a select-option control that appears on multiple pages, so I created a single page to contain the select-option and then included that page in other pages. The issue I am facing is that when I use this component on page 1 and update the selected values in page 2 using inputs and outputs, any changes made in page 2 do not reflect back in page 1 upon closing page 2. Can someone help me resolve this issue? Here's what I have tried so far....

custom-nav-bar.html

<ion-toolbar>
  <ion-navbar primary>

        <ion-item text-wrap>
          <ion-select interface="popover" (ionChange)="onChange()" [(ngModel)]="choose">
            <ion-option [value]="cg" *ngFor="let cg of chArr;" >{{cg.fname}}</ion-option>
          </ion-select>

        </ion-item>
      </ion-navbar>
</ion-toolbar>

custom-nav-bar.ts

@Component({
  selector: 'page-custom-nav-bar',
  templateUrl: 'custom-nav-bar.html',
  inputs: ['chArr', 'choose'],
  outputs: ['optionpageselection']
})
export class CustomNavBarPage {

  choose: any;
  optionpageselection: EventEmitter<string> = new EventEmitter<string>();

  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad CustomNavBarPage');
  }


  onChange() {
    console.log("onchange called in custNav")

    this.optionpageselection.emit(this.choose);

  }
}

page1

export class page1 {
      choose: any;

onChange(event) {
    this.choose = event;
    console.log("you have selected")
    console.log(this.choose);
  }
}

page1.html

<ion-header>

  <page-custom-nav-bar [chArr]="chArr" [choose]="choose" (optionpageselection)="onChange($event)" ></page-custom-nav-bar>
</ion-header>


<ion-content padding>
</ion-content>

page2.html

<ion-header>

  <page-custom-nav-bar [chArr]="chArr" [choose]="choose" (optionpageselection)="onChange($event)" ></page-custom-nav-bar>
</ion-header>

<ion-content>
</ion-content>

page2.ts

export class page2{
  chArr: any;
  choose: any;

constructor(public navCtrl: NavController,
    public navParams: NavParams, private storage: Storage,
    public alertCtrl: AlertController) {

    this.chArr = navParams.get('chArr');
    this.choose = navParams.get('choose');
    console.log('choose ' + this.choose);


  }
onChange(event) {
    this.choose = event;
    console.log("you have selected in page2")
    console.log(this.choose);
}
}

Answer №1

Mother

onPageLoad() {
    this.userInfo = this.routeParams.get('userData') || null;
}

Offspring

onPageExit() {
    this.routeControl.getPrevious().data.userData = this.userInfo;
}

Answer №2

If you're looking for an alternative approach, consider utilizing the Storage module.

custom-nav-bar.ts

With the onChange method, you can store a value using this.storage.set('choose', this.choose) which can be accessed from any part of the application.

page2.ts

In the ionViewDidEnter lifecycle hook in page2.ts, retrieve the stored value by calling this.storage.get('choose').then((val) => { let choose = val; });

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

tinyMCE5 has a quirky habit of inserting p tags without warning, but only when using

My editor with tinymce5 works well on Chrome and other browsers, but in Mozilla Firefox each line in the editor gets an additional p tag. I am using it with Angular. Currently, I have initialized the editor like this: <editor name="resultEditor" ...

Version 1.0 of the Angular 4 branch integrated with Auth0 JWT

I have been trying to implement the code provided on this website https://github.com/auth0/angular2-jwt/tree/v1.0 However, I am facing an issue with the following code snippet that needs to be placed in the app.module.ts file. The problem is that this co ...

Guide on utilizing external namespaces to define types in TypeScript and TSX

In my current project, I am working with scripts from Google and Facebook (as well as other external scripts like Intercom) in TypeScript by loading them through a script tag. However, I have encountered issues with most of them because I do not have acces ...

How can I troubleshoot TypeScript in Ember CLI using VSCode?

After doing some research on stackoverflow, I came across this post: How to debug Javascript-Code produced and served by ember-cli? That post was created back in 2014, so I'm hoping that there have been updates or new features added since then. I&ap ...

Having trouble with importing ResizeSensor library into typescript

In my TypeScript app using webpack, I am attempting to utilize css-element-queries/ResizeSensor. After adding the npm package, which includes a .d.ts file, I encountered an issue when writing the following code: new ResizeSensor(element, cb); Instead of ...

Tips for customizing the appearance of Angular Material select drop down overlay

In my project, there is an angular component named currency-selector with its dedicated css file defining the styling as shown below: .currency-select { position: absolute; top: 5px; right: 80px; z-index: 1000000; color: #DD642A; f ...

Having trouble converting the JSON object received from the server into the necessary type to analyze the data

I am new to angular and struggling with converting a JSON object from the server into a custom datatype to render in HTML using ngFor. Any help would be appreciated as I have tried multiple approaches without success. Apologies for the simple HTML page, I ...

Guide to dynamically update a div element based on selection changes using Angular 2 dropdown menu

This is my unique html template <form #animalForm="ngForm"> <div class="animal-profile-search"> <input type="text" class="animal-profile-search-bar" placeholder="Enter Animal Name"> <select [(ngModel)]="animal" class="animal- ...

Intellisense in Typescript does not provide mapping for the Pick type

The Typescript Pick type is not displaying intellisense mappings in vscode (or stackblitz). When using Pick<MyType, 'someProperty'> to define a type with a documented property of MyType, hovering over or trying to navigate to the definition ...

Exploring Angular 5's capabilities with elevate-zoom usage

Check out this amazing Jquery Image Zoom Plugin! Having some trouble with the elevate-zoom jquery plugin in my angular 5 app. Getting this error message: ERROR TypeError: this.elevatezoomBig.nativeElement.elevateZoom is not a function https://i.ssta ...

Modify the BehaviorSubject upon clicking or focusing on the input

I have created a directive for an input field. I want to trigger a flag in another component when the input is clicked or focused upon. @Directive({ selector: '[appDatepicker]' }) export class DatepickerDirective implements DoCheck{ constru ...

The Typescript Compiler API Type Checker produces varying types from what is perceived by the Typescript Language support within the VS Code environment

Utilizing the Typescript Compiler API type checker, I am analyzing identifier nodes in a loaded file to determine their types within a program. To begin, I load the file and initialize the program and type checker like so: const program = ts.createProgram ...

Achieving checkbox values in Typescript: A guide

I need help with capturing the values of checked checkboxes and storing them in a string to use in my API. I want to retrieve the value if a checkbox is unchecked. <div *ngFor="let x of groupesTable"> <input type="checkbox" [(ngModel)] ...

Obtain the query parameter before undergoing redirection within Angular version 14

In my Angular application, I am facing a challenge where I need to display a specific message to the user once they click on a link that has been sent to them via email. The link is intended to open a page within the app at the URL: "localhost:8080/?d ...

Generate a binary string using JavaScript and then transform it into C#

I have an upload section in my JavaScript program. I utilize JS FileReader to obtain a binary string of the uploaded document before sending it to my C# WebApi for storage on the server. JavaScript Code let myFile = ev.target.files[0]; if(myFile.size > ...

What causes the app to crash in release mode when importing a TypeScript component, while no issues arise in debugging?

Having an issue with importing a bottom sheet written in typescript into a class component. It works correctly in debugging mode but unfortunately not in release mode. Despite checking the logcat, no readable error code or message is being printed. Even a ...

Animated scrolling in Angular

I am working on a webpage using Angular, where each module is a component with an animation. However, I am facing an issue where the animation only runs when the page loads, but I want it to happen while the component is visible on the screen. One approa ...

Adding new fonts to an Angular 2 web page

I am working on an Angular 2 application and I want to incorporate wrapbootstrap. However, I am facing an issue with the fonts (bootstrap, font-awesome, google) and I am not sure how to include them. While using the css file for wrapbootstrap, I am gettin ...

Why am I encountering an issue while trying to access req.user.id?

Having set up passport authentication on my express, node project, I encountered an error when trying to access req.user. The error message displayed is Property 'id' does not exist on type 'User'.ts(2339). Below is the relevant code sn ...

Applying these sorting functions to my specific scenario

Hello everyone, I hope you can help me out with this issue. I have been struggling for hours trying to sort my array of objects in JavaScript based on a specific property, but I just can't seem to get it right. I have referred to some of the top post ...