Dynamic Routing in AngularJS based on Checkbox Selection

Currently, I am in the process of mastering Angular JS by working on an HTML Sample project. My goal is to create a user-friendly form where users can input basic information and select checkboxes to load different form pages using UI Routing. The page should dynamically generate navigation links based on the checkboxes selected. Once the form is filled out, it should be saved in a server directory and downloaded to the user's computer.

Although I was able to display all the data as a JSON array in the form, I'm encountering issues after trying to incorporate checklist links for navigation and saving features.

In my App.js file:

I've set up our Angular app and included ngAnimate and ui-router as dependencies

angular.module('formApp', ['ngAnimate', 'ui.router'])

.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider

    .state('form', {
        url: '/form',
        templateUrl: 'form.html',
        controller: 'formController'
    })

    .state('form.profile', {
        url: '/profile',
        templateUrl: 'form-profile.html'
    })

    .state('form.interests', {
        url: '/interests',
        templateUrl: 'form-interests.html'
    })

    .state('form.payment', {
        url: '/payment',
        templateUrl: 'form-payment.html'
    });

$urlRouterProvider.otherwise('/form/profile'); })


.controller('formController', function ($scope) {

$scope.prefContactArray = [];
// code continues...

});

In Form.html:

In Submit Button Page:

In Index:

Answer №1

Within your create() function, you are utilizing

$state.go($scope.selected.fruitsList.url)
, which will navigate to a new state. However, it is important to note that the value being passed is a template path rather than a state path.

To ensure correct functionality, consider using

$state.go($scope.selected.fruitsList.state)
. The 'to' parameter of $state.go() should represent the name of the state to transition to or a relative state path. If the path begins with ^ or ., it is considered relative; otherwise, it is absolute.

Answer №2

$state

In order to navigate through your application, it is essential to utilize the $state service provided by UI Router. By adding this dependency to your controller like shown below:

.controller('formController', function ($scope, $state) {

You will have the ability to easily transition from one state to another within your application using commands such as $state.go('form.payment').

When handling form submissions, make sure to determine the appropriate state to navigate to and conclude with $state.go(stateToGoTo) in your code.

Pro Tip:

For optimal organization and clarity in your routing structure, consider creating a separate route for each individual page instead of utilizing nested children routes. This approach allows you to assign dedicated controllers to each route, streamlining the code separation process and enhancing code readability.

Have you found this information 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

Preventing Bootstrap Modals from closing or refreshing on page reload

My code contains multiple buttons that trigger the function showModal(title). showModal(title) { this.modalTitle = title this.$bvModal.show("add-modal") } Below is the specific modal being targeted. <b-modal id="add-modal" ...

Using @carbon/react in conjunction with Next.js version 13 leads to unconventional styling

Here's what I did to set up my Next.js application: npx create-next-app@latest I then installed the necessary package using: npm i -S @carbon/react The global styles in app/globals.scss were customized with this code snippet: @use '@carbon/reac ...

Using Express in Node.js to send a GET request to a different server from a Node route

When a client triggers a GET request by clicking a button on the index page, it sends data to a route that is configured like this: app.js var route = require('./routes/index'); var button = require('./routes/button'); app.use('/ ...

Utilize Axios to send data in real-time to update any changes made to an input field in

Just wanted to write about this topic because I have a burning question. (Please note that the code in this post is just an example). I'm struggling with sending the input content of name_contact on each change without using a button. Is it even poss ...

conceal the .card-body element if the children have the CSS property "display:none"

My challenge involves managing two collapsible cards on a webpage. I am looking for a solution where the .card-body will have its display set to none when there are no inner divs to show in the card upon clicking a letter in the pagination. Otherwise, the ...

Is this code correct for passing a variable to another form?

$("#delete").click(function() { deleterecord(); }); function deleterecord(){ var id = $("#iduser").val(); alert("aw"+id); var id = $('#iduser').attr(); e.preventDefault(); pressed = "delete" $.ajax({ ...

Emails can be sent through a form without the need for refreshing

I am currently working on a webpage that utilizes parallax scrolling, and the contact box is located in the last section. However, I encountered an issue where using a simple HTML + PHP contact box would cause the page to refresh back to the first section ...

How to Toggle Div Visibility with AngularJS ng-show and ng-click

In order to eliminate error messages, I decided to clear the field with a button click. This approach successfully removed the error in my initial test. <div class="errorPopup" data-ng-click="runner.Name = null" data-ng-show="mainForm.name.$error.pat ...

Three.js - creating transparent materials that only display the background, not the inner sides of objects

I am interested in applying a transparent material to the front-side faces of a geometry. It's a fairly simple process: var normal = new THREE.MeshNormalMaterial(); normal.side = THREE.BackSide; var materials = [ norma ...

"Trouble arises as Amchart and Ajax encounter data that is not in the

I am trying to utilize AmChart and retrieve data for charts from PHP. Below is my PHP file: $colors = Array("#FF0F00","#FF6600","#FF9E01","#FCD202","#F8FF01","#B0DE09","#04D215","#0D8ECF","#0D52D1","#2A0CD0","#8A0CCF","#CD0D74","#754DEB","#DD ...

Providing static content within the generated code structure by Yeoman for the Angular Fullstack framework

I'm sticking to the code structure generated by Yeoman for an Angular fullstack application. The issue I'm facing is how to include a script called core.js in a file named app.html. <script src="core.js"></script> I can't fi ...

Is there a way I can modify the display setting to show 4 slides per view?

var swiper = new Swiper(".new-arrival", { slidesPerView: 4, centeredSlides: false, spaceBetween: 30, autoplay: { delay: 5500, disableOnInteraction: false, }, pagination: { el: ".swiper-pagination", type: &qu ...

Sending a Form Generated in Real Time with a Button Located Outside the Form

I'm currently working on a feature that allows users to add multiple invoices on a single page. Users can click on a link to display the invoice fields in a modal form, which is dynamically generated using AJAX. Once the user has filled out the requi ...

Click on the link to open in a new tab and redirect the current window to a different website

Currently, my form action is set to "_blank" in order for the pdf document I am generating to open in a new tab/window. While this works correctly, I am looking to also have the original page redirect to a different URL simultaneously. Are there any ways ...

The JavaScript if statement does not appear to be accurate

I am encountering issues with my if, else, and else if statements. Here is the code snippet in question: function draw(d) { var testarray = JSON.parse(a); var testarray1 = JSON.parse(a1); var testarray2 = JSON.parse(a2); var Yaxis = $(" ...

How can we use JavaScript to add a class to the <html> element?

What is the method to apply a class to the root element <html> in JavaScript? ...

Tracker.gg's API for Valorant

After receiving help with web scraping using tracker.gg's API and puppeteer, I encountered an error message when the season changed {"errors":[{"code":"CollectorResultStatus::InvalidParameters","message":" ...

Tips on searching for an entry in a database with TypeScript union types when the type is either a string or an array of strings

When calling the sendEmail method, emails can be sent to either a single user or multiple users (with the variable type string | string[]). I'm trying to find a more efficient and cleaner way to distinguish between the two in order to search for them ...

When attempting to make a GET request from AngularJS to Phalcon using the $http method, the params array

I am currently using Angular 1.6.2 with Phalcon framework. $scope.selectAction = function(Item){ $http({ method: 'GET', url: 'Mycontroller/new', params : {Item : Item}, headers : {'Accept' : 'application/json&ap ...

Interfacing Node JS with Java Web Services via SOAP

I've been attempting to connect Java web services from a Node.js module, but I'm encountering an error in the wsdl library. Below is my wsdl file: <!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130 ...