Navigate to a different state with ui-router upon entering the page

I am implementing two wizard flows, flow A and flow B, each collecting different sets of information.

    states["wizard"] = {
        abstract: true,
        url: "^/wizard",
        controller: "wizardController",
        templateUrl: "wizard.html"
    };

    states["wizard.input"] = {
        url: "^/input",
        views: {
            "": {
                controller: "wizardController",
                templateUrl: "form.html"
            }
        }
    }
    states["wizard.completed"] = {
        templateUrl: "completed.html"
    };

When a user navigates to /wizard, I need to check if informationB is filled. If not, I want to redirect the user to fill informationB first and then return to /wizard to continue with wizardA.

What is the best approach to handle the redirection when the user navigates to /wizard? And how can I smoothly transition back to /wizard once wizardB is completed?

One idea I have is passing a parameter in the URL for the return from wizardB to wizardA. However, I am struggling to determine the best method for the initial redirect.

Thank you for your help!

Answer №1

Appreciate the info, charlietfl! Using resolve for handling redirects seems like a solid approach. Here's my implementation:

const redirect = (returnState: string) => {
    return ['$q', '$timeout', '$state', 'dataModel', ($q, $timeout, $state, dataModel) => {
        const deferred = $q.defer();
        $timeout(() => {
            if (dataModel.informationB === "") {
                $state.go('wizardB.input', { rs: returnState });
                deferred.reject();
            } else {
                // All good to proceed
                deferred.resolve();
            }
        });

        return deferred.promise;
    }];
}

And in the state configuration:

resolve: {
    redirect: redirect("wizardA.input")
}

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

Display a recurring list within an Ionic Pop Up

I am facing an issue with a collection repeat list that includes a search bar at the top of the list. When displayed on a real Android 4.4 device, only 9 records are showing up. I have created a codepen example here, where all the records are displayed co ...

prior to activating a state in angular.js, navigate to a distinct controller

Upon loading my website, I have a specific state in mind that I want to be redirected to. Achieving this is made possible through the following code snippet. angularRoutingApp.run(function ($rootScope, $state, $location, $transitions) { $transitions.o ...

Display the value beyond the ng-repeat loop

Is there a way to display the text {{Brand.BrandDetails.Text}} just once outside of the ng-repeat, while still allowing the repetition? <li ng-repeat="Brand in Brands"> <a href="#"> <img src="~/Images/{{Brand.BrandDetails. ...

A guide on displaying JSON response data in Angular JS with the help of ng-repeat

I am attempting to display the values of a specific JSON in the correct order. Here is how my JSON is structured : { "A":[{"id":"21","name":"Andrea"},{"id":"22","name":"Apple"}], "B":[{"id":"21","name":"Baby"},{"id":"22","name":"Bali"}], "C":[{"id":"21"," ...

Incorporating an SVG with CSS styling from a stylesheet

After exploring various articles and questions on the topic, I am yet to find a definitive solution. I have an external file named icon.svg, and my objective is to utilize it across multiple HTML files with different fill colors and sizes for each instanc ...

Encountering a problem with $state.go in conjunction with ui-router: the URL successfully transitions to the new state, but the HTML content for the new view fails to

I'm facing a situation where I have an index.html page with two views: View1.html on the left and view2.html on the right. However, when a record is selected in the left view, I want to switch the right view from view2.html to view3.html. Currently, ...

`Need help setting the active class for a bootstrap navbar using Angular JS?`

In my bootstrap navbar, I have the following menu items: Home | About | Contact I'm looking to assign the active class to each menu item based on the current angular route. Specifically, how can I set class="active" when the angular route is at # ...

Tips for dynamically creating column headings and table values using JSON arrays in AngularJS

On a web page, there are two radio buttons that produce different results: The first button shows Student Details and the corresponding JSON array is as follows : [{"Name":"A", "Class":"x", "Section":"abcd", "DOB": "XYZ"}, {"Name":"B", "Class":"x", "S ...

Tips for improving performance with ng-repeat directive?

I have encountered some performance issues while using socket.io with the ng-repeat directive in Angular. The application slows down significantly when receiving a large amount of data from the backend, making it impossible to interact with the app. What w ...

Exploring the FormData Object in Mongoose

Currently, I am in the process of developing a geolocationAPI that would allow users to query locations based on their specified minimum and maximum distance. However, I am facing difficulties in retrieving these values within my controller. Here is a sni ...

calculating the size of an array in node.js

I received a form object from my AngularJS to Node.js and this is how it looks in the console: For two files, filenames : [object Object],[object Object] # filenames object for two files length: 2 # used object.length For one file, filenames : [object ...

What methods can be utilized to avoid displaying a Bootstrap popover intermittently within an AngularJS framework?

When using the bootstrap popover in a custom AngularJS directive, I need to display an error message when the button is disabled by setting $scope.buytypeButton= {type:false}. The error message should be displayed in a popover. On the other hand, when $sco ...

Challenge with Alignment of Fields in Bootstrap's Horizontal Form

I am working on an Angular project with a horizontal form using Bootstrap. I have encountered a slight alignment issue with one of the date fields. The IsMessedUp datepicker input field is not aligned properly, positioned slightly to the left compared to t ...

Using AngularJS Typeahead with restrictions on $http requests

I have been attempting to restrict the number of results displayed by Angular Bootstrap Typeahead during Async calls, but unfortunately, it does not seem to be functioning as expected. <input type="text" ng-model="asyncSelected" placeholder="Locations ...

The Angular JS controller does not function correctly when it is wrapped within $(function () {});

After enclosing the controller code within the $(function () {}); method, it ceased to function properly. Below is an example of the code: Contacts.cshtml @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

Creating a series of text messages for Push Notifications using FCM in conjunction with Ionic 1. Multiple lines of

I've been attempting to send push notifications with multiline text messages. I've experimented with various techniques such as using setBigStyle in FCMService.java, HTML.fromHTML, and others, but haven't been successful in getting the messa ...

Using the base tag in Angular HTML5 mode can cause script links to break

Issue Following the configuration of a base tag to enable Angular 1.x in HTML5 mode, encountering an error where the browser (Chrome) sends requests to an incorrect path for scripts when directly accessing the app via localhost:3000/resource/id. Specific ...

The AngularJS dependency injection system allows developers to easily manage dependencies using arrays and the $inject

Being new to angularJS, I'm seeking some insight on dependency injection. After some research, here's what I've gathered: I have 2 service files (using factories): -mogService.js angular.module('anglober.services').factory(&apo ...

Why is the location search not staying centered after resizing the map on Google Maps?

I am currently working on integrating Angular with Google Maps. I need to add some markers along with location search functionality. Additionally, I am including location information in a form. When the addMarker button is clicked, a form opens and the map ...

What is the best way to fill a dropdown menu with names and corresponding numeric IDs?

How can I effectively link my dropdown to a variable in the controller, using the id property of the dropdown array instead of the value for assignment? Meanwhile, still displaying the content of the drop down using the name property of the array for user ...