Forgetting local variables after a routing redirect

Struggling with my first CRUD Application using AngularJS.

I've successfully created PHP web services for CRUD operations.

The issue arises when trying to edit a specific user (object in ng-repeat).

The goal is to transfer the user data from page (listUsers.html) to another page (addUser.html) in order to display their properties in input fields.

Currently, I have only one controller and am utilizing routing.

For example:

In ListUsers.html, there is a button that triggers the update() function on click and redirects to AddUser.html:

app1.controller('testController',function($scope,$http){

    $scope.update=function(e){
        $scope.message="testeeee";
        $scope.newStudentt = e;
    };
});

How can I pass the value of $scope.message to AddUser.html?

Answer №1

Utilize a service to transfer data seamlessly:

Check out the PLUNKER

app.controller('ListCtrl', function($scope, UserService, $location) {
  // Retrieve user information from the server
  $scope.users = [
    {name: 'Jesse', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a707f69697f5a7f627b776dc775157d7774">[email protected]</a>'},
    {name: 'Walter', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f68181b6938e979b869af080969495968496ff8082">[email protected]</a>'}
  ];

  $scope.update = function(user){
    UserService.setUser(user);
    $location.url('/user/edit');
  };
});

app.controller('EditUserCtrl', function($scope, UserService, $location) {
  $scope.user = UserService.getUser();

  $scope.save = function(){
    // Add saving functionality here
    alert('Saved');
  };

});

app.service('UserService', function(){
  var editingUser;
  this.setUser = function(user){
    editingUser = user;
  };

  this.getUser = function(){
    return editingUser;
  }
});

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

Angular detects when a user scrolls on a webpage

I have developed a straightforward navigation system using Angular. The main controller is responsible for generating the menu. <nav class="{{active}}" ng-click= ""> <a href="#a" class="home" ng-click= "active='home'">Home< ...

Is it possible to develop an asynchronous function using Javascript?

Take a look at this code snippet: <a href="#" id="link">Link</a> <span>Moving</span> $('#link').click(function () { console.log("Enter"); $('#link').animate({ width: 200 }, 2000, function() { c ...

Javascript: A Fun Game of Questions and Answers

When using JavaScript exclusively, I have an array consisting of four questions, four correct answers, and four incorrect answers. The use of arrays is essential to maintain order in the data. As each question is displayed, a random number is generated by ...

What is the best method to adjust an element's height depending on the heights of two other elements?

Is there a way in React to make the height of one component equal to the sum of the heights of two other components on the page? I am working with responsive elements in my application built with React and Typescript. I have attempted using refs on the t ...

Having problems with the For...In syntax in Javascript?

The search feature in this code snippet seems to be causing some trouble. I suspect that the issue lies within the For...In loop, however, my knowledge of JavaScript is still pretty new. Here is the snippet: var contacts = { john: { firstName: "john ...

Error: The carousel in Bootstrap is throwing a TypeError because f[0] is not

We are currently utilizing Bootstrap Carousel to load dynamic slides, with each slide corresponding to an item in an array. AngularJS is employed to create the array and iterate through it. However, during execution, we encountered a javascript error Type ...

Using HTML and JavaScript to verify email addresses

I have been working on an "Email Validation" code, but it seems to be malfunctioning. I can't figure out why. Would you mind taking a look at it? Thank you. I suspect that the issue lies with this line document.getElementById("custEmail").onchange = ...

Click events failing to trigger within an HTML dropdown menu

Within my Angular view, I have created an HTML dropdown like the one below: HTML <select> <option value="Func 1"> <button class="btn ui-button ui-widget ui-state-default ui-corner-all" ng-click="callFunc1( ...

Refreshing the browser causes AngularJS to disregard any cookies that have been set

Building an AngularJS single-page application with SQL, Node.js, and Express that incorporates login functionality using Passport and basic authentication. Once logged in, users can access routes to edit items in the database successfully. However, there s ...

customize Form Modal Bootstrap with AJAX Chained Dropdown for pre-selected options

I am facing an issue with a chained dropdown on a bootstrap modal, Here is the code snippet: function changeDetail(id) { var id = id; $('#edit').prop('hidden', true); $('#modal_form').prop('hidden', fa ...

What is the best way to add items to arrays with matching titles?

I am currently working on a form that allows for the creation of duplicate sections. After submitting the form, it generates one large object. To better organize the data and make it compatible with my API, I am developing a filter function to group the du ...

Does Highchart offer support for drilling down into sub-categories?

I want to implement a sub-sub drill down feature in my Chart using the following code snippet. // Create the chart Highcharts.chart('container', { chart: { type: 'column' }, title: { text: 'Highcharts m ...

Unlock the lightbox and send the user to the parent page

Is there a way to simultaneously launch a lightbox popup and redirect the parent page? I have an AJAX script that retrieves HTML content as a response. My goal is to display this content in a lightbox popup while also directing the parent window to a des ...

jQuery Hide/Show Not Working as Expected

I am currently developing a Single Page Application using jQuery along with Semantic and Bootstrap for the UI. I have encountered an issue where jQuery is struggling to hide and show two elements on the same level, even though it works fine elsewhere in th ...

Using a conditional statement in JavaScript, create a mapping between the values in an array and string

I have a dropdown list that I want to populate with options. The functionality of the onchange event is handled by the following code snippet: const handleChange = (event) => { onFilterChange(filterName, event.target.value); } The value of event.ta ...

Addressing an error of "call stack full" in nextjs

I am currently working on a project in nextjs where I need my billboard to continuously scroll to show different information. The Nextjs debugger keeps showing me an error message that says 'call stack full'. How can I resolve this issue? con ...

I'm trying to retrieve information from openweathermap in order to show it on my app, but I keep running into an error that says "Uncaught RangeError: Maximum

I recently created a div with the id 'temporary' in order to display data retrieved from the openweathermap.org API. However, I am encountering an error in the console and I'm not sure why. This is my first time working with APIs and I would ...

turn off event listener in vue

I am trying to figure out how to remove the event listener in this scenario. Since I am calling a method from within the event listener function, I need to use ES6 syntax and can't use named functions. How can I go about removing the event listener? ...

AngularJS Understanding the Scope in HTTP GET Requests

As a newcomer to AngularJS, I am on a mission to decode the conventions of this code: Check out the code here I am tweaking it to utilize a REST service for fetching messages instead of relying on the messages array. This is what the MessageService code ...

Creating fundamental forms using HTML, CSS, and Javascript

I am tasked with creating a unique web application where users can sketch and annotate simple shapes. The purpose of the app is to create basic store maps. These shape drawings must be saved in a database, including their coordinates, sizes, labels, and cu ...