AngularJS - Creating a dynamic wizard experience using UI-Router

I have set up my routes using ui-router in the following way:

$stateProvider
    .state('root', {
        url: "",
        templateUrl: 'path/login.html',
        controller: 'LoginController'
    })
    .state('basket', {
        url: "/basket",
        views: {
            '': {
                templateUrl: 'path/basket.html',
                controller: 'BasketController'
            },
            'address@basket': {
                templateUrl: 'path/address.html',
                controller: 'AddressController'
            },
            'confirmation@basket': {
                templateUrl: 'path/confirmation.html',
                controller: 'ConfirmationController'
            },
            'payment@basket': {
                templateUrl: 'path/payment.html',
                controller: 'PaymentController'
            }
        }
    });

In the basket.html file, I am using mgo-angular-wizard to create a "step bar".

<wizard on-finish="finishedWizard()">

<wz-step title="Address">
    <div ui-view="address"></div>
</wz-step>

<wz-step title="Confirmation">
    <div ui-view="confirmation"></div>
</wz-step>

<wz-step title="Payment">
    <div ui-view="payment"></div>
</wz-step>

Everything is working as expected so far. However, I now need to add buttons that will allow navigation between these pages. For instance, the Address page should have a button that navigates to the Confirmation page. I have seen examples online using

ui-sref="$state.go('basket.address')"
, but since I'm not using this structure with dots, I am unsure how to achieve this (using confirmation@basket is not working).

I am relatively new to Angular and learning as I go. Thank you in advance.

Answer №1

Personally, I believe that Ui.Router is all you need in Angular for seamless state navigation without losing any state (you can store data in services).

It's beneficial to implement nested states...

Here are the steps:

  1. Start by creating the parent abstract state: Basket. This will allow child states to inherit views, resolves, and URL fragments.
  2. Create child states: address; confirmation; payment

angular
  .module('checkout', ['ui.router'])
  .config(function($stateProvider) {
    
    var basket = {
      name: 'basket',
      url: '/basket/',
      abstract: true,
      views: {
        "main": {
          template: "<h1>Bye</h1>",
          controller: function() { alert('bye'); }
        },
        "sidebar": {
          template: "<ul><li>LINK</li></ul>",
          controller: function() { console.log('sidebar view'); }
        }
      }
    };
    
    var address = {
      name: 'basket.address',
      url: ''
    };
  
    var confirmation = {
      name: 'basket.confirmation',
      url: 'confirmation/',
      views: {
        "main@": {
          template: "<h1>ss</h1>",
          controller: function() { alert('ss'); }
        }
      }
    };
      
  
    var payment = {
      name: 'basket.payment',
      url: 'payment/',
      views: {
        "main@": {
          template: "<h1>zz</h1>",
          controller: function() { alert('zz'); }
        }
      }
    };    
  
    $stateProvider
      .state(basket)
      .state(address)
      .state(confirmation)
      .state(payment)
    ;
  })
;
<section ui-view="main"></section>
<aside ui-view="sidebar"></aside>

<ul>
  <li><a ui-sref="basket.address">Address</a></li>
  <li><a ui-sref="basket.confirmation">Confirmation</a></li>
  <li><a ui-sref="basket.payment">Payment</a></li>

</ul>

  1. Utilize a service through angular.module('checkout').value to store all checkout progress.

If you want to consolidate views and states into one, consider using Ui.Router.extra known as STICKY STATES, the link provides various examples...

Trust this information proves helpful!

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

Having trouble with the ::after element in my React component for my latest React 3D portfolio project

For my portfolio project, I am following a tutorial by LamaDev (https://www.youtube.com/watch?v=qALsVa-V9qo&t=2334s&ab_channel=LamaDev). At around timestamp 36:40, he creates a ::after selector which is not working for me, even though the rest of t ...

How can I retrieve the parent scope in an Angular UI nested named view?

One of the challenges I faced in my application was dealing with nested views within several screens. After experimenting with different approaches, I discovered that creating modal dialog boxes and sliding panels as actual nested states with corresponding ...

Pass Form ID To Function In A Dynamic Way

I have multiple forms on my webpage and I want to use the same ajax function for all of them. It currently works well for one form by fetching the id with getElementById and then passing it to the ajax function. My goal is to dynamically pass down the form ...

Javascript - Single line conditional statement

As I continue to improve my JavaScript skills, I'm looking for guidance on optimizing the following if statement. Is there a way to shorten it, possibly even condense it into one line? How can I achieve that? onSelect: function (sortOption) { th ...

Selecting a dropdown value dynamically after a form submission in ColdFusion using jQuery

I created a basic form with the use of an onload function to populate market values by default. However, I encountered an issue where after selecting a market value from the drop-down list and submitting the form, the selected value does not stay selected ...

Setting limits to disable or remove specific times from the time picker input box in Angular

I'm having trouble with a time input box. <input type="time" min="09:00" max="18:00" \> Even though I have set the min and max attributes to values of 09:00 and 18:00 respectively, it doesn't seem to be working properly. I want to ...

What is the best way to implement a seamless left-to-right sliding effect for my side navigation using CSS and Javascript?

I am currently working on creating a CSS and JavaScript side navigation. Below is the code that I have implemented so far: var nav = document.getElementById('nav'); function show(){ nav.style.display = "block"; } .side-nav{ background-color:bl ...

React is failing to display identical values for each item being mapped in the same sequence

I have implemented some standard mapping logic. {MEMBERSHIPS.map((mItem, index) => ( <TableCell className="text-uppercase text-center" colSpan={2} padding="dense" ...

How can we effectively translate intricate nested SQL relationships into manageable services using front-end data modeling best practices?

Seeking insights on potential solutions to a complex issue. Angular and Rails will be used, but this problem is more abstract and doesn't have to be addressed within these frameworks. What are the optimal methods for managing intricate nested SQL ass ...

Dynamically fetching data with Node.js using Ajax requests

Despite my efforts to scour Google and Stack Overflow, I have been unable to find a reliable method for posting data to my Node.js server. I've noticed conflicting information on various methods, likely due to changes over time. One particular code ...

How come the values keep appearing without any loops or subsequent calls being made to the PHP file?

Here is a demo example showcasing 'Server Sent Events(SSE)': Embed HTML code(index.html) : <!DOCTYPE html> <html> <body> <h1>Receiving server updates</h1> <div id="result"></div> <script> if(type ...

Steps for inserting a menu item into a Material UI Card

Below is an example I'm working with: https://codesandbox.io/s/material-ui-card-styling-example-lcup6?fontsize=14 I am trying to add additional menu options such as Edit and Delete to the three dots menu link, but so far I have not been successful. ...

The dropdown hack on ui.bootstrap.typeahead is compromised when multiple input fields are used in AngularJS

I attempted to enhance ui.bootstrap.typeahead with a hack that adds a dropdown functionality. I discovered the hack on this site. The modification allows the dropdown to appear when clicking inside the input field. Although it functioned appropriately with ...

Retrieve the subdomain from within the passport FacebookTokenStrategy and GoogleStrategy

In the process of developing a node.js application with wildcard subdomains, I am creating separate parts of the app for each subdomain that will have distinct user authentication based on the subdomain. For instance, envisioning my app having subdomains ...

Comparison: NumberFormatter versus NumberFormat in PHP and JavaScript

My attempts to format currency seem to yield inconsistent results. Using PHP with the NumberFormatter class, here's a snippet of my code: $number = 5125.99; echo getInternationallyFormattedCurrency($number, 'tl-PH', 'PHP'); echo & ...

The translation of popups using ngx-translate in Javascript is not functioning properly

When I click on my request, the content "Are you sure?" is not changing to the required language. This issue is located in list.component.html <button type="button" (click)="myrequest(item.Id)">Request View</button> The fu ...

Tips for eliminating the backslash introduced by JQuery

Switching back from framework 4.5 to 4.0 caused several issues that needed fixing. One significant change I noticed was that jQuery started escaping double quotes. Is there a way to stop this behavior? I attempted datatest = datatest.replace("\\ ...

Using various designs on elements generated from ng-repeat

Is it possible to apply different styles to buttons created using ng-repeat by having b.color return a value from the btnValues object? Currently, it is not returning the value. If this is not possible, are there alternative methods to achieve this? < ...

Using $location in an Angular directive causes a cascade effect of repainting all other directives

I am encountering an issue with my page where multiple directives are being used: <section class="start_page page--no-vcenter"> <u-search> <u-search-form></u-search-form> <u-news-form></u-ne ...

Opt for a line break over a semicolon when using jQuery

I need assistance with breaking the line wherever a semicolon is present. var locdata = { "locations": [{ "id": 0, "name": 'USA', "cx": 100, "cy": 100, "address":'545, 8t ...