What is a practice for utilizing navCtrl.push() with a variable storing a class name?

Currently, I am utilizing Visual Studio Code for Ionic 3 development with AngularJS/Typescript. In my code, I am using this.navCtrl.push() to navigate to different pages within the application. Specifically, I have two classes/pages named "level1" and "level2" that I need to dynamically navigate to.

this.navCtrl.push(level2)

When directly specifying the class/page name like above, everything works smoothly. However, when attempting to use a variable:

levelNbr: any;

if(value == null)
{
   this.levelNbr = "level1"
}
else
{
   this.levelNbr = "level2"
}

this.navCtrl.push(this.levelNbr)

An error is encountered stating:

Uncaught (in promise): invalid link: level2

This suggests that although it identifies "level2", it fails to open the respective page. Is there a way to successfully navigate to a page by using a variable?

Thank you

The following code snippets were used:

import { level1 } from './../levels/levels';
import { level2 } from './../levels/levels';
//all classes are in one file called levels

In the app.modules.ts file, they are also declared.

Answer №1

When loading new components or pages, the process is similar to using "lazy loading" for those components. However, if you are encountering an invalid link error, it means lazy loading is not being utilized in this case.

To resolve this issue, ensure that relevant components are imported into the Component you are working from and assign them accordingly:

import { Level1Component } from '../whatever";
import { Level2Coomponent } from '../whatever2";

...
if(value == null)
{
   this.levelNbr = Level1Component
}
else
{
   this.levelNbr = Level2Component
}

this.navCtrl.push(this.levelNbr)

Make sure that these imported Components (levels) are correctly declared in your app's overall module (app.module.ts)

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

Create custom AngularJS directives for validation and store them in a variable

Through the use of AngularJS, I've developed a directive called "integer" that invalidates a form if anything other than integers are entered. Because I'm generating the page dynamically by fetching data from the database, it would be helpful to ...

"Embracing the power of Spring Boot with AngularJS

I'm attempting to create a Spring Boot application. I have already set up the project, but I'm encountering an issue with my controller code: @RestController public class IndexController { @RequestMapping(value="/home",method = RequestMetho ...

Generating JSON objects within the Ionic SQLite framework

I am new to Ionic development and I'm looking for a way to convert a JSON string into a JSON object in Ionic, as well as how to access this JSON data on an HTML page. controller.js app.controller('OilTrackerListingCntrl', function($scope, ...

Testing Angular controllers with mocked methods is an essential part of unit testing in Angular

I'm facing a challenge with mocking the method of service in the controller. While I know how to mock simple services like service.method, I am unsure about how to mock "action.user.update". Whenever I try to spy on it, I encounter an error saying &ap ...

Terser is causing ng build --prod to fail

When I run ng build --prod on my Angular 7 application (which includes a C# app on the BE), I encounter the following error: ERROR in scripts.db02b1660e4ae815041b.js from Terser Unexpected token: keyword (var) [scripts.db02b1660e4ae815041b.js:5,8] It see ...

Are MEAN stacks and boilerplates suitable for custom applications requiring specialized functionality?

After spending a significant amount of time learning and coding, I have encountered a common issue. You can find the link to the discussion below. Instead of reinventing the wheel, should I utilize existing boilerplates that combine Express, Angular, conn ...

I am disappointed with the lack of functionality in Angular's HTML type inference

When working inside an Angular component, I want to select a div element by id or class. This method works menuDisplayDiv = document.getElementsByClassName("some_class")[0] as HTMLDivElement; menuDisplayDiv = document.getElementById("some ...

What are some alternatives to utilizing ng-init within a 'view'?

I am currently working on implementing a 'like' feature for my application. My goal is to dynamically set the value of a generated number as the 'like count'. However, I have encountered an issue with using 'ng-init', as it is ...

Is there a possibility in TypeScript to indicate "as well as any additional ones"?

When working with typescript, it's important to define the variable type. Consider this example: userData: {id: number, name: string}; But what if you want to allow other keys with any types that won't be validated? Is there a way to achieve thi ...

AngularJS service functionality fails to execute in straightforward app

Having trouble with my simple Angular app, can't seem to figure out what's wrong. The code is available on Plunker at the following link: http://plnkr.co/edit/QQkP2HB6VGv50KDdBPag?p=preview. It's throwing an error: Uncaught Error: [$injector ...

Retrieve: Type 'string | undefined' does not match the parameter type 'RequestInfo'

When using the fetch function, I encountered an error with the "fetchUrl" argument: Error: Argument of type 'string | undefined' is not assignable to parameter of type 'RequestInfo'. This is the code snippet where the error occurred: ...

After utilizing Geolocation, the input fields in Ionic/Angular JS are failing to update accurately

Currently, I am in the process of developing a new App using Ionic and Angular JS. Specifically, in one of the app tabs, I am utilizing geolocation to populate four fields (street name, street number, latitude, and longitude). Below is the snippet of my c ...

Handling events in React using TypeScript

Currently diving into the world of React with Typescript and encountered a challenge involving event handling using the onClick property. I have a react component displaying a list of items from an array, and I aim to log the clicked item in the console. I ...

In Angular, when a promise returns an "undefined" value, how does this interact with .NET

When I execute this code snippet, I am encountering an issue where the response returned is "undefined" instead of the expected value. Here is the code in question: $scope.SameNameFunction = function() { var payload = { itemname: $scope.EventD ...

Angular 1.3.10 encountered error: [$injector:modulerr] - Uncaught Exception

The application currently has limited content, resulting in this basic error. The use of ui-router is causing the issue. Here are the code snippets from different files: app.module.js: 'use strict'; angular .module('app', app); ...

experiencing an excessive amount of rerenders when trying to utilize the

When I call the contacts function from the main return, everything seems fine. However, I encounter an error at this point: const showContacts = React.useCallback( (data: UsersQueryHookResult) => { if (data) { return ( < ...

Angular - The filter method cannot be used on Observables

I have a database with users who need to complete unique homework sessions. Each homework session has its own identifier, name, date, and other details. After fetching all the user's homework from the database, it is stored in a private variable call ...

Monaco Editor in TypeScript failing to offer autocomplete suggestions

When using a union type as a parameter of a function in Monaco Editor, the suggestions do not appear. However, in VS Code, the suggestions are provided. Is there a setting I need to enable in Monaco to have the same functionality? Although Monaco provides ...

Is the original functionality of Bootstrap fully preserved in BootstrapUI?

Having recently delved into the world of the bootstrap framework, I find myself drawn towards using AngularJS instead of the default jQuery provided by Bootstrap. The process for incorporating BootstrapUI seems straightforward - simply downloading the code ...

Displaying Angular charts can enhance the visualization of data by mapping chart labels to the properties of an object

Currently, I am utilizing angularJs Charts which can be found at In my possession is an array of objects that looks something like this: let MyArray = [{a: 1, b: 2,c:3}, {a: 3, b: 4,c:10}, {a: 5, b: 6,c:20}, {a: 7, b: 8,c:30}]; The goal here is to connec ...