Store information in Factory and retrieve it in the controller

I am encountering an issue with my code.

Below is the factory code:

.factory('shareDataService', function() {
    var sharedData = {};

    sharedData.shareData = function(dateFrom, dateTo) {
        var from = dateFrom;
        var to = dateTo;
        alert(from + to);
    };
    return sharedData;
})

And this is the controller:

.controller('getFormDataCtrl', ['$rootScope','$scope', '$http', 'shareDataService', function ($rootScope,$scope, $http, shareDataService) {
  $scope.getFromBase = function () {
    shareDataService.shareData($scope.dateFrom, $scope.dateTo)       
  }

  $scope.bookingFormSubmit = function (){
    var array = {
        "from": ''// i want put dateFrom HERE,
        "to": '',// i want put dateTo HERE
        "yacht": $rootScope.yacht_id,
        "customer": {
            "fistname": $scope.firstname,
            "lastname": $scope.lastname,
            "birthday": $scope.birthday,
            "phone": $scope.phone,
            "email": $scope.email,
            "country": $scope.country,
            "city": $scope.city
        }
    }
  }}])

This page is developed using Symfony2 and Symfony2 forms. The initial form allows customers to select a start date and end date, with a button labeled "getFromBase()".

I aim to store this data in order to utilize it at a later time.

The subsequent form is displayed in another UI view. Here, customers input their personal details (such as name, last name, etc.). Upon clicking "bookingFormSubmit()", I retrieve all the data from this form. I also need to incorporate the data from the factory (the two variables) to create a JSON object.

Answer №1

This specific information contained in the service is considered sensitive. In order to access it, you must broaden the scope and make use of an accessor.

Data Service

.factory('shareDataService', function() {
    var sharedData = {};
    var from, to;

    sharedData.getSharedData = function ()
    {
      return {from: from, to: to};
    };

    sharedData.shareData = function(dateFrom, dateTo) {
        from = dateFrom;
        to = dateTo;
    };

    return sharedData;
})

Instructions for Use

.controller('getFormDataCtrl', ['$rootScope','$scope', '$http', 'shareDataService', function ($rootScope,$scope, $http, shareDataService) {
  $scope.getFromBase = function () {
    shareDataService.shareData($scope.dateFrom, $scope.dateTo)       
  }

  //NEW
  var data = shareDataService.getSharedData;

  $scope.bookingFormSubmit = function (){
    var array = {
        "from": data.from, //NEW
        "to": data.to,     //NEW
        "yacht": $rootScope.yacht_id,
        "customer": {
            "fistname": $scope.firstname,
            "lastname": $scope.lastname,
            "birthday": $scope.birthday,
            "phone": $scope.phone,
            "email": $scope.email,
            "country": $scope.country,
            "city": $scope.city
        }
    }
  }}])

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

The functionality of item.classList.toggle() in HTML+CSS+JS fails to execute

I have been working on a program that aims to flip a card when it is clicked. The JavaScript code I have written for this functionality is as follows: /* Card flipping onclick */ import "./Stylesheets/FlipCardStyle.css" var cards = document.quer ...

How can I troubleshoot jQuery not functioning in iOS even though it works on Safari?

Trying to implement jQuery on a website using xCode simulator for testing, but experiencing issues. Works fine on Safari webkit though. Click here to view the site. I would appreciate finding out what's causing the problem, as well as advice on how t ...

What is the best way to convert an array of data into a dataset format in React Native?

Within my specific use case, I am seeking to reform the array structure prior to loading it into a line chart. In this context, the props received are as follows: const data = [26.727, 26.952, 12.132, 25.933, 12.151, 28.492, 12.134, 26.191] The objective ...

html issue with angularjs progress bar not displaying progress updates

I'm currently working on implementing a progress bar in Angular and updating its progress dynamically. Here is a snippet of the code I am using: <progressbar value="{{data.progress}}"></progressbar> Below is the controller code that I ha ...

Achieving consistent text alignment in HTML and CSS without relying on tables

As I delve into the world of HTML and CSS, I've taken a traditional approach by crafting a login page complete with three input controls (two text inputs and one button). To ensure proper alignment of these elements, I initially turned to the trusty & ...

The flickering problem with HTML5 DOM

I created a HTML5 game using canvas and DOM elements, but I'm facing an issue with flickering DOM elements while playing. The problem seems to be more prevalent in mobile browsers, particularly Chrome. My game includes a full screen canvas with vario ...

I'm encountering an issue with my React 18 application using TypeScript: the module './App' cannot be found

Encountering an issue while attempting to update to react 18. I'm curious if this problem is related to my file types. I am using Typescript, so do both the app and index files need to have a .tsx extension? Both the app and index files are located ...

Encountered a SyntaxError: Unexpected token "e" while attempting to parse a JSON string

When I attempt to use JSON.parse in order to convert the string below into a JavaScript object, I encounter an "Uncaught SyntaxError: Unexpected token e" error. { "__type": "HRIS.oHRData, HRIES, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", ...

Different boolean variable assigned to every item in v-for loop

I am working on creating a custom play/pause button for my audio elements, and here is how I have implemented it: <div v-for="(post, p) in post_list"> <!-- ... --> <!-- ... --> <!-- ... --> <v-avatar v-i ...

Is there a straightforward way to upload a folder in a ReactJS application?

I am searching for a way to upload a folder in ReactJS. I have a folder with .doc and .docx files in it, and I want the user to be able to upload the entire folder when they click the browse button. I need to prevent the user from selecting individual fi ...

I'm trying to understand a JSON object that has multiple key names pointing to a single value. How can I properly interpret this in

Encountering an object with a strange key. const obj = { formValues: { 'TOTAL;_null_;_null_;3;_null_': "100" 'billing;_null_;_null_;1;_null_': "Y" 'billing;_null_;_null_;2;_null_': "Y" 'billi ...

Seeking guidance on recompiling a directive within Angular?

I have implemented a directive to display data in a tree-like structure. This directive allows for filtering of the data. However, when I clear the filter, the directive does not automatically update to reflect the changes in the data. It only updates when ...

What's the best way to display a bootstrap modal window popup without redirecting to a new page?

I am having trouble implementing a modal window that will display validation errors to the user when they submit a form. Currently, the window is opening as a new view instead of overlapping the existing form's view. How can I adjust my code so that t ...

Error: The system is unable to destructure the 'username' property from 'req.body' since it is not defined

Encountering a persistent issue with an error that I've been trying to resolve for days without success. The problem arises when attempting to register a user in the project, resulting in error messages being displayed. Here's a snippet of the co ...

What is the best way to assign an index to each ng-repeated item in order to use it for a ng

Is there a way to implement ng-click in order to retrieve the current index of a thumbnail image in a gallery? I have a gallery with thumbnails, and when a thumbnail is clicked, I want the main image to display the full version corresponding to that thum ...

Tips for showcasing JSON data within an array of objects

I'm trying to work with a JSON file that has the following data: {"name": "Mohamed"} In my JavaScript file, I want to read the value from an array structured like this: [{value: "name"}] Any suggestions on how I can acc ...

Tips for ensuring the security of your code in node.js

Here is a snippet from my app.js that deals with managing connections: var connections = []; function removeConnection(res) { var i = connections.indexOf(res); if (i !== -1) { connections.splice(i, 1); } } I make a call to removeConn ...

Strategies for restricting user input within a datalist

I have a project in which I am creating a webpage that displays a list of flights that can be filtered by destination, origin, price, and more. To achieve this, I have categorized each flight within a div element using its specific properties as classes. F ...

JQuery Ajax encounters a 500 error message due to an internal server issue

I'm currently using the jQuery library to send an ajax request to a PHP file. Initially, everything was working perfectly fine with a relative path like this: url:"fetch_term_grades.php", However, when I modified the path to be more specific like th ...

Navigating after submitting a form using the Location header in jQuery

Seeking a way to utilize the Location header in jQuery 1.7 for redirection to a target. The current code is structured as follows: $('#creationLink').click(function(){ $.ajax({ type: 'POST', url: '/', success: ...