Manipulating variables in a page from a different page in Angular 7: How to control a variable in dashboard.page.ts from landing-in.page.ts

Hey, I'm a newcomer to Angular 7 and Ionic 4. How can I manage variables on the dashboard page from another page?

I attempted a simple logic in my application by controlling tbFor with the setToolbar function at dashboard.page.ts from landing-in.page.ts, but it was unsuccessful and resulted in an error:

(ERROR Error: Uncaught (in promise): Error: StaticInjectorError (AppModule) [LandingInPage -> DashboardPage] :).

However, this issue seemed to resolve when I imported dashboard.page.ts into app.module.ts and included it in providers. Strangely, when I logged the setToolbar results as "true," upon page load, the value of the variable tbFor remained "false".

Here's an example snippet from dashboard.page.ts for reference:

import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { MenuController } from "@ionic/angular";
import { AuthService } from "../services/auth/auth.service";
@Component({
 selector: "app-dashboard",
 templateUrl: "./dashboard.page.html",
 styleUrls: ["./dashboard.page.scss"]
})
export class DashboardPage implements OnInit {
 tbFor = {
   divCS: false,
   divGA: false,
   divTC: false
 };

 constructor(
   public router: Router,
   public menu: MenuController,
   private authService: AuthService
 ) {
   this.initializeApp();
 }

 initializeApp() {
   this.menu.enable(true, "sideMenu");
 }

 setToolbar(div) {
   if (div == "CS") {
     this.tbFor.divCS = true;
   } else if (div == "GA") {
     this.tbFor.divGA = true;
   } else if (div == "TC") {
     //Ignores
     this.tbFor.divTC = false;
   }
 }

 clearToolbar() {
   this.tbFor.divCS = false;
   this.tbFor.divGA = false;
   this.tbFor.divTC = false;
 }
}
...

Answer №1

Utilize services to store and manage shared variables effectively. Simply create a new service and include it as a provider within your module.

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

Guide on integrating two separate UI (CSS) frameworks, one optimized for mobile and the other for desktop, within a single Angular project

I am tasked with developing both mobile and desktop versions of a web application using two different CSS frameworks, Ionic 4 and Bootstrap, for different use cases. Despite these differences, the rest of the app remains the same, utilizing Ngrx store and ...

Using dots instead of lines for the carousel indicators in PrimeNG

I'm currently working on a carousel feature, but I want to change the indicators from lines to small dots. I know the solution lies within the CSS files, but I'm not sure how to implement it. I think I need to create a new CSS class, but I could ...

Navigating Tabs in Angular 4 with ngx-bootstrap and the Router

I am facing an issue with my tab navigation using the router. Only clicking directly on the link switches the tab and updates the route. However, if you click around or below the link, the tab switches but the route does not update. It seems like the issue ...

"Utilize PrimeNG's p-tabpanel to bind a unique style class to

When using the tabview component from PrimeNG, I am encountering an issue where I am unable to bind a header style class. Here is my static HTML code that works: <p-tabPanel header="Title" headerStyleClass="badge" formGroupName=&quo ...

Return a potential undefined output

I am working with a variable called root which could potentially be undefined. Its value is only determined at runtime. const root = resolvedRoot || await this.fileSystem.getCurrentUserHome(); console.log('root.uri = ' + root.uri); The existenc ...

Is Angular's forkJoin being phased out?

Upon opening my Angular project today, I came across a warning message that said: The utilization of forkJoin is marked as deprecated: resultSelector is deprecated, it is recommended to use pipe to map instead (deprecation) https://i.sstatic.net/vFpeu.pn ...

Loss of flexbox sizes in Angular Material Drag and Drop when items are dropped

I'm currently working on implementing drag and drop functionality to my list using Angular Material CDK Drag and Drop. However, I've run into an issue where the alignment of the items is lost once dropped, causing them to be displayed differently ...

Using the .json method in Angular 7 for handling responses

While attempting to implement the function getProblems to retrieve all problems within its array, I encountered an error message appearing on res.json() stating: Promise is not assignable to parameters of type Problem[]. It seems that the function is ...

In TypeScript, having *at least* one property is mandatory

Imagine having an interface like this: export interface IAlert { cta?: CTA; description?: string; title?: string; } How can you set up the typing so that at least one property is required to be passed in, but still allow for all ...

What is the best way to utilize mat-paginator for handling multiple requests instead of fetching all the data at once?

In our project, we have a view that pulls all the necessary information (in JSON format) for the mat-paginator display. However, there is an issue with speed as the list grows daily with thousands of new rows. One potential solution is to fetch data from ...

Utilizing Carousel Functionality in Angular 4

I've added a Carousel to one of my components. Bootstrap has been correctly imported (verified by testing a bootstrap button). The active image, slider icons, and navigation icons at the bottom are all visible. However, the images are not sliding and ...

How to update an Angular 2 component using a shared service

My question is regarding updating components in Angular 4. The layout of my page is as follows: Product Component Product Filter Component Product List Component I am looking to link the Product Filter and Product List components so that when a user c ...

A notification won't appear unless the window is clicked

After creating the GlobalHandlerError service, I noticed that errors are being displayed properly in the console. However, when I added angular-notifier, notifications only show up after a mouse click in the current window. Each notification requires a s ...

angular change digits to Persian script

I am trying to convert English numbers to Persian numbers in angular 4: persianNumbers = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"]; engli ...

BehaviorSubject keeps reverting back to its initial value

I'm currently working on an Angular 2 project that consists of multiple components. One particular component is responsible for keeping track of a member using a subscription to a BehaviorSubject. Whenever a user clicks a button, the value of the Beh ...

How to remove the parent.parent.parent element when a button is clicked in Angular without utilizing the element's ID

I am currently developing a website where users should have the ability to delete li-elements with just one click. These li elements are contained within bootstrap containers, which means that when deleting, I want to remove the entire container along with ...

AngularFire2 version 5.0 entities

As I navigate through AngularFire2, starting with version 5.0 and encountering numerous recent changes, I find that much of the information available online is outdated. My main goal right now is to retrieve an object from my Angular database and pass its ...

In Angular 5, when you reset a required form control in a reactive form, the required error message beneath the input field is not cleared

Within my template, there is a form that becomes visible when a button is clicked- <form [formGroup]="person" (ngSubmit)="onSubmitNewPerson()" #formDirective="ngForm"> <mat-form-field> < ...

Creating a FormGroup dynamically using an observable: A step-by-step guide

My current project involves creating a page with multiple reactive forms, tailored for different countries. These forms are generated based on JSON arrays received from the backend, allowing users to view and update settings individually. As I am uncertain ...

Dealing with extensive fixtures in Cypress

Scenario: Currently, the task at hand involves setting up end-to-end tests for a web application that relies on a rather complex API. In order to test just one aspect of the site, approximately 15 REST requests need to be intercepted. The API in question ...