Tips for sending an array of objects to a parameter in $stateParams with angular-ui-router

I am working on a form that looks like this:

$scope.forms = [{}, {}];

<div classs="" ng-repeat="each_form in forms"> 
 <input class="" model="each_form.name"> Name
 <input class="" model="each_form.email"> Email
 <input class="" model="each_form.phone"> Phone Number
</div>

Currently, I am passing $scope.forms to $stateParams by including all the data in the parameters:

$state.go('form_details', {params: forms});

==my app.js

        .state('form_details', {
            url: '/form_details?params',
            templateUrl: 'views/form_details.html',
        })

However, passing data this way results in an unreadable URL. Therefore, I am wondering if there is a way to pass this form data in a more readable format. I hope my question is clear.

Answer №1

The most efficient method is to create a specialized Service:

.factory('CustomService',[function(){
    var items = {};
    return {
        set: function(data){
            items = data;
        },
        get: function(){
            return items;
        }
    }

}]);

Then, utilize it in your controller by injecting it first: var items = CustomService.get() to retrieve the information

CustomService.set(data) to store the data

Answer №2

Is there a way to make form data readable when passing it?

Unfortunately, no.

You are limited to passing primitive values such as strings, integers, and booleans.

One workaround is storing the data in a service under a unique key and then passing that key in the URL for easy retrieval.

Here's an example:

$scope.models = [{}, {}];
var uniqueKey = SomeService.setData($scope.models);
$state.go('class_report', {key: uniqueKey});

In another controller:

$scope.models = SomeService.getData($stateParams.key);

The code snippet for the SomeService would look like this:

app.service('SomeService',function () {

   var self = this;
   self.models = {}; 

   self.setData = function(models){
     self.models[key] = models;
     return key;
   } 

    self.getData = function(key){
     return self.models[key]
   } 

})

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

JavaScript - Attempting to retrieve data using AJAX

Struggling with extracting data from an AJAX call using jQuery while implementing RequireJS for better maintainability and modularity in my JavaScript. Still new to RequireJS so not entirely sure if I'm on the right track. Just aiming to keep my JS mo ...

Upon executing `$('div')`, an empty array is returned

My goal is to retrieve all the div classes on a page using jQuery $('div');. However, the page does not natively support jQuery, so I am loading it through external links until $ === jQuery evaluates to true. Even on Stack Overflow where jQuery ...

Applying a filter to an element variable within the Angular JS framework

I'm currently working on an ng-repeat where I've implemented a filter: #1 <div class="contestReports" ng-repeat="contest in contests | filter:{votingOver:true}"> <contestreport></contestreport> </div> However ...

Getting a page element by its id with QWebEngineView is a simple task that can be

Is there a way to access the page ElementById in order to input a value? import sys from PyQt5 import QtWebEngineWidgets from PyQt5.QtCore import * from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import * from PyQt5.QtWidgets import QAction from PyQt ...

Retrieve isolated scope of directive from transcluded content

I am not certain if it is possible, but I am essentially looking for a reverse version of the '&' isolate scope in AngularJS. You can check out this Plunkr to see an example. In essence, I have created a custom directive that provides some r ...

What is the best way to dynamically populate a table with JSON data that is received from an API response?

Currently, I have a table that contains the FORM element: <table id="example" class="sortable"> <caption><h3><strong>Product inventory list</strong></h3></caption> <thead> <tr ...

Triggering a JQuery click event programmatically using another click event

Below is the code snippet: <label style="display: inline; " for="default"> <img class="ful" src="http://www.mysite.com/default.png" /> <input style="display:none" id="default" name="selected" type="radio" value="template" onclick="SubmitVal ...

Utilizing jQuery to extract JSON data from various URLs and insert it into multiple div elements

I have an HTML list containing 4 items, each with its own assigned id. My goal is to retrieve JSON data from multiple URLs using AJAX for each item in the list and then parse the data into the respective item's id. Expected outcome: Each item receive ...

Function for calling a CSS callback with jQuery

I am currently working on enhancing my search bar using jQuery, and I am also looking to hide the navigation links. Here is a snippet of the jQuery code I have been using. The code functions properly when focused. $(".searchBox input").focus(function(){ ...

Which specific web framework supports the functionalities of Microsoft Dynamics CRM 2011?

Is there an SDK available from Microsoft that can help me create a web product with similar rich ajax features as Microsoft Dynamics CRM 2011? I have considered using Microsoft SharePoint Foundation 2010, but I am concerned that it is designed for small o ...

What is the process of connecting a Yarn module to a Docker container in another repository?

I'm currently facing a challenge in linking a module to a Docker container from another repository. To provide some background, I have a container hosting a React application named launch-control-admin. This project relies on a yarn module called @com ...

JavaScript Library function in Angular Component throwing Type Error: Function is Not Recognized

I created a custom Javascript library to group together essential functions that many Angular Components require. The library called calcs.js includes functions like this: function calculateCosts(object) { do some stuff..... return answer; } To use t ...

Tips for utilizing the multiselect() function without deleting current values and incorporating new values determined by another selection

Currently, I am working on an e-learning project that involves managing a large number of student registrations. I have implemented a search box to find students' names and add them to a list. However, I encountered an issue when trying to search for ...

Setting new query parameters while maintaining existing ones without deleting them in Next.js v13

"use client"; import { useCallback, useEffect, useState } from "react"; import Accordion from "../accordion/accordion"; import { useRouter, usePathname, useSearchParams, useParams, } from "next/navigation"; i ...

Converting JSON to CSV using Node.js

Looking to convert JSON data to a CSV file in Node.js? Wondering if there are any predefined modules for this task? P.S. Additionally, interested in incorporating formatting such as including headers like "MONTHLY REPORT" and row colors? ...

Having trouble accessing information from Firebase Realtime Database within a React Native application

I am currently developing a React Native application that interacts with a Firebase database for reading and writing data. I have configured my Firebase permissions to allow read and write access: { "rules": { ".read": true, ...

How can the name of an element be passed as an argument to a function called by that element within a Vue component?

I'm currently developing an interactive SVG map inspired by the one found on the CDC page. In my SVG map component, I have implemented multiple path elements that each contain a unique name attribute representing the corresponding state. Additionally, ...

Is it possible to write a text file in UTF-16 using Javascript?

I need to create a UTF-16 text file from a string. For instance: var s = "aosjdfkzlzkdoaslckjznx"; var file = "data:text/plain;base64," + btoa(s); The above code generates a UTF-8 encoding text file. How can I modify it to produce a UTF-16 text file usin ...

JavaScript or Query: Transforming an Object from an Associative Array

Can someone help me out with converting an associative array into an object in JavaScript? I tried searching on Stackoverflow but couldn't find a working example. Here is the example structure: var combinedproducts = [["Testing-1","test-1"],["Testin ...

Ensure that you provide distinct values for both the model and view in your input

Is there a way to automatically format the numbers I type with a $ symbol in the input field, without actually storing the $ in the model? I've tried different methods but so far, if there is already a value in the model (not null), it displays with $ ...