Passing data between screens in AngularJS

I'm currently working on a project that involves developing a cross-platform app using Onsen UI, Monaca, and AngularJS.

Within this app, I have 2 screens. The initial screen requires users to input a Vehicle ID, which triggers an API call. This API call fetches data based on the provided Vehicle ID, and then populates the API URL with this value. Subsequently, on the next screen, a list of options returned as a JSON object corresponding to the Vehicle ID is displayed.

For example, the API call structure is as follows: mymadeupdomain/api/vehicle.php?id=109

Here, 109 represents the ID entered by the user to determine the content displayed on the detailed vehicle screen.

Although I have hardcoded some values and can retrieve the JSON object returned by the API call, I'm encountering difficulty in dynamically sending the values when users enter them.

The form in vehicle-id.html retrieves the vehicle ID from the user:

<form class="login-form" style="text-align: center" name="myForm">
    <section style="padding: 8px">
        <input type="text" 
        class="text-input--underbar" 
        required 
        minlength="3" 
        maxlength="10" 
        ng-model-options="{ debounce : 800 }"
        placeholder="Vehicle ID / Fleet Number" 
        ng-model="fleetIDSearch" >
    </section>
</form>

The app.js file acts as the controller for processing the form submission:

angular.module("myApp", ['onsen']).controller("vehicleController", function($scope, $http)
{
    // Watch for changes on the vehicle ID screen
    $scope.$watch('fleetIDSearch', function()
    {
       fetchFleetDetails(); 
    });

    $scope.fleetIDSearch = "";

    function fetchFleetDetails()
    {
        $http.get("http://mymadeupdomain/api/vehicle.php?id=" + $scope.fleetIDSearch).success(function(data)
        {
            $scope.fleetIDs = data;
        });
    }

    // Retrieves a list of all Vehicle Checks associated with Vehicle ID
    $http.get("http://mymadeupdomain/api/getfleetchecks.php?fleetid=" + $scope.fleetIDSearch).success(function(data) // NOT WORKING HERE
    {
        $scope.checkItemDescriptions = data;
    });
});

My main challenge lies in passing the values entered in $scope.fleetIDSearch = ""; from the first screen to the second screen's API URL. How can I accomplish this seamlessly?

Ultimately, my goal is to showcase a comprehensive list of checks linked to the specified ID, as presented below (currently functional with hardcoded API URL):

vehicleCheck.html

<ul class="list">
    <li class="list__item" ng-repeat="checkItemDescription in checkItemDescriptions">
        {{checkItemDescription.checkitemdesc}}
        <label class="switch switch--list-item">
            <input type="checkbox" 
                class="switch__input" 
                checked >
            <div class="switch__toggle"></div>
        </label>
    </li>
</ul>

Answer №1

There are various options available for managing data between controllers at different levels.

If you need to share data simply between controllers, consider using Factories

Alternatively, you can:

  • Set an ng-model on your initial page
  • Upon form submission, redirect to a URL like /content/:id
  • Handle the request (retrieve remote data) within the router resolver
  • Access the result directly in the second controller of the subsequent page

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

What is the best way to organize these checkboxes using BootstrapVue's layout and grid system?

My BootstrapVue table setup looks like this: https://i.sstatic.net/K3Rwy.png This is the code for the table: window.onload = () => { new Vue({ el: '#app', computed: { visibleFields() { return this.fields.filter(field ...

Implementing a powerful multi-role authorization system in React

I am currently developing an application with multiple user roles (admin, user, manager). I am trying to restrict access to the admin route from both managers and general users, and also render the UI based on the user's role. I have attempted this bu ...

Having difficulty with JSON.Parse - console is showing an error in the DOCTYPE

I'm having trouble writing JSON data to a div using JQuery: var obj = JSON.parse(data); $('#container').html(obj.services.service[0].code); However, I keep receiving a Syntax error related to my DOCTYPE: <!DOCTYPE HTML PUBLIC "-//W3C// ...

Arranging array positions in ThreeJS

My BufferGeometry contains an array of x/y/z positions with almost 60,000 points (18,000 values), [3, 2, 1, 3, 2, 1, 3, 2, 1, ...] To obtain random points, I am considering shuffling these positions and then selecting the first 30,000. One idea is to fir ...

Calculate the total number of pages within an epub document

As a beginner in the world of epub, I have acquired numerous files in different epub formats and now wish to make them easily readable online. I'm not quite sure what an epub file contains. Is there a method to determine the number of pages in my epub ...

Dealing with uploading files using multipart form submission in a custom remote method of Loopback

I've exhausted all options suggested in How can I use body-parser with LoopBack? without success. The client app I'm working on is built with AngularJS and utilizes the ng-file-upload module, implemented as shown below: Upload.upload({ ...

Is it possible to dispatch actions from getters in Vuex?

Fiddle : here Currently, I am in the process of developing a web application using Vue 2 with Vuex. Within my store, I aim to retrieve state data from a getter. My intention is for the getter to trigger a dispatch and fetch the data if it discovers that t ...

What is the best way to iterate through an object and display each item as <li></li> within another <li>?

After retrieving a specific object from an express endpoint and seeing it on the console, I need to figure out how to map it in order to display a jsx that resembles this <li>keys</li> : <li>values</li> For example: Company: DML ...

Having issues extracting information from easports.com due to difficulties with the <ea-elements-loader> element

Recently, I've been developing a Python WebScraper to extract data (such as wins and losses) from our FIFA ProClub by crawling various websites. While I successfully implemented it on a third-party website using BeautifulSoup and requests, I encounter ...

Seeking information on determining the actions performed when DOMContentLoaded is triggered in JavaScript?

Interested in obtaining all the code that is triggered when DOMContentLoaded event is fired or jQuery's $(document).ready() function? I am looking to improve my website's performance, and after running a speed test, I discovered that 2 seconds o ...

The error message "mapboxgl is not defined" indicates that the mapboxgl variable

I have been encountering the following error message in the console, and despite spending hours trying to troubleshoot it, I have been unsuccessful in resolving the issue. Could someone assist me in fixing this problem? error image I am having difficulty ...

Guide on building a Dynamic factory in AngularJS

For my project, I need to implement a dynamic factory in AngularJS with a unique name. Here is an example of what I am trying to achieve: function createDynamicFactory(modId) { return myModule.factory(modId + '-existingService', function ...

Incorporating a rigged and animated asset into a preexisting scene using Three.js

Currently, I'm developing a browser app similar to Tamagotchi using three.js. However, I've hit a roadblock while trying to implement a hand that can pet the avatar when clicked. The Hand is a rigged Blender model with two animations: idle and p ...

Invoking AngularJS Function from Login Callback Script

Just getting started with angularjs and I have a logincallback function that is used for external login. This function returns the returnUrl, closes the externallogin pop up, and redirects back to the main page. function loginCallback(success, returnUrl) ...

The request to retrieve data from the model at http://localhost:9000/model/data.json resulted in a 404

This is the path to my directory I have a npm server running on http://localhost:9000/ to utilize the cytoscape module. However, my server is unable to locate my json file. What could be the issue with my code? This is the source code of my index.js file ...

"Error TS2339: The property specified does not exist within type definition", located on the input field

When a user clicks a specific button, I need an input field to be focused with its text value selected entirely to allow users to replace the entire value while typing. This is the markup for the input field: <input type="text" id="descriptionField" c ...

Ways to create interactive multiple dropdown menu using vue-multiselect

I'm not sure if it's possible to achieve what I want with Vue for a specific component by changing its data and automatically loading it. Below is my expectation (tried in jQuery) var data = {country:{type:'dropdown',values:[' ...

Iterating through a series of Axios requests nested within each other to handle pagination in an

I need help iterating through the API response that includes pagination. Below is a snippet of the API response: { count: 165, next: "http://example.com/name?offset=30&per_page=30", previous: null } Here is my useEffect hook: const [datas, se ...

Useragent-dependent styling conditions

How can I select the appropriate stylesheet based on the useragent? For instance, I would like to display a specific css style for Android and another for iPhone. Is it achievable using only css? Can media queries be utilized? Appreciate any help in ad ...

Tips for Resizing and Reviving Divs with jQuery

I have recently ventured into the world of web development to assist a family member with their website. My knowledge and experience are limited, but I am facing an interesting challenge. I am trying to manipulate certain divs as users scroll down the page ...