Electric LocalData

I am currently developing an app that includes a To Do List feature. I am having trouble with saving the tasks and would appreciate some help. I want the tasks to be automatically saved every time you click on "Create Task" and should also be displayed every time you open the app. Below is the Popup section with the "Create Task" button:

Popup

$scope.newTask = function() {
  $ionicPopup.prompt({
    title: "New Task",
    template: "Enter Task:",
    inputPlaceholder: "What do you need to do?",
    okText: 'Create Task'
  }).then(function(res) {    // promise 
    if (res) $scope.tasks.push({title: res, completed: false});
  })
};

Answer №1

One solution is to utilize LocalStorage. Check out this resource for more information:

Answer №2

To store data locally, you can use localStorage in the following way:

$scope.saveData = function() {
  $ionicPopup.prompt({
    title: "Save Data",
    template: "Enter Data:",
    inputPlaceholder: "Input your data here",
    okText: 'Save'
  }).then(function(res) {    // using promise 
    if (res) 
       var randomNumber = Math.floor((Math.random() * 100) + 1);
       var data = {value: res, stored: false};
       window.localStorage.setItem("Data" + randomNumber, JSON.stringify(data));

  })
};

Retrieve the data in your controller like this:

$scope.getData = function() {
   for (var key in localStorage) {
      $scope.dataArray.push(JSON.parse(localStorage[key]));
   }
};

In your view, trigger the function with ng-init as shown below:

 <ion-content ng-init="getData()">

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

Escaping an equal sign in JavaScript when using PHP

I am currently working on the following code snippet: print "<TR><TD>".$data->pass_name."</TD><TD><span id='credit'>".$data->credit_left."</span></TD><TD><input type='button' val ...

Error: an empty value cannot be treated as an object in this context when evaluating the "businesses" property

An error is occurring stating: "TypeError: null is not an object (evaluating 'son['businesses']')". The issue arose when I added ['businesses'][1]['name'] to 'son' variable. Initially, there was no error wi ...

What is the best way to swap out particular phrases within HTML documents or specific elements?

Trying to update specific strings within an HTML document or element. How do I go about parsing and replacing them? To clarify: - Identify all instances of #### in element .class - Swap them out with $$$$$ Currently utilizing jQuery for this task. Appre ...

Displaying Product Attribute and Category Names in Woocommerce Title

After reading the answer provided in this thread (Woocommerce: How to show Product Attribute name on title when in a category page and "filtering" products via '?pa_attribute=' on address bar), I am interested in displaying both the categ ...

What could be the reason for the empty response in my PATCH request in Javascript?

I am facing an issue with my app that runs Rails in the backend and Javascript in the frontend. The controllers, routes, and CORS are all set up correctly. Both my Post and Get requests work without any problems. However, when I attempt to make a patch req ...

Passing a list variable to JavaScript from Django: A step-by-step guide

Currently, I am facing an issue while attempting to generate a chart using Chartjs and Django. The problem arises when transferring data from views.py to the JavaScript code. Here is a snippet of my code in views.py: def home(request): labels = [&quo ...

Can jQuery and Google Analytics be loaded together in a single process?

My current setup includes the following: <script src="http://www.google.com/jsapi?key=..." type="text/javascript"></script> <script> //<![CDATA[ google.load('jquery', '1.6'); //]]> </script> &l ...

How can I use AJAX to remove {{$key}} in Laravel?

In our codebase, we are utilizing multiple hidden inputs within a loop structured like @foreach. <form action="{{ route('parties.updateAndCreate', auth()->id()) }}" method="post"> @csrf @method('PUT' ...

Activating the CSS :active selector for elements other than anchor tags

How can I activate the :active state for non-anchor elements using JavaScript (jQuery)? After reading through Section 5.11.3 of the W3C CSS2 specification in relation to the :hover pseudo selector in hopes of triggering the activation of an element, I stu ...

What is the best way to implement asynchronous image loading on hover events in React.js?

You may have come across this type of effect before. A good example can be found here - https://codepen.io/anon/pen/GEmOQy However, I am looking to achieve the same effect in React. While I understand that I can use the componentDidMount method for AJAX c ...

Differences between CORS HTTP requests initiated by a browser and those from a locally hosted web application

I am currently working on a small proof-of-concept web application that needs to send a GET request to a server running SAP ABAP system and providing ODATA REST Services. Interestingly, when I manually enter the Services' URIs in the browser's ad ...

Optimizing Array Comparison in JavaScript and Angular 2

In my Angular 2 .ts (TypeScript) file, I have declared two arrays as shown below: parentArray: Array<Model> initialized with {a, b, c, d} modifiedArray: Array<Model> modified with data {c, e, f, g} How can I efficiently determine the differ ...

When accessing the route "/[locale]", make sure to await the `params` object before utilizing its properties like `params.locale`

Currently, I am developing a Next.js 15 application utilizing the new App Router (app directory) with dynamic route localization. Within my project, I have implemented a [locale] directory to manage multiple language routes and utilize the params object to ...

Enhancing the efficiency of typed containers in JavaScript

Recently, I uncovered a clever method for creating fake 'classes' in JavaScript, but now I'm curious about how to efficiently store them and easily access their functions within an IDE. Here is an example: function Map(){ this.width = 0 ...

Sprucing up the Angular-UI Bootstrap modal

I am currently working on a feature that closely resembles the functionality provided by the Angular-UI Bootstrap Modal, with one key difference: The modal I am building needs to be positioned relative to a specific container div (styling handled through ...

What is the process for making a POST request with the Google Chrome Puppeteer library?

Hello everyone, I'm currently attempting to make a POST request using the puppeteer headless chrome library. I'm running into some issues with the code below and can't seem to figure out what's wrong. // Obtain the csrf token ...

Transfer an array generated within a JavaScript function to a PHP script using another function

I've successfully created an array within a Javascript function, and now I'm looking to pass this array to a PHP script upon clicking a button on the HTML page. My attempt to pass it involves calling the following function: function transferarra ...

Is there a way to prevent a link from activating when I click on one of its internal elements?

Within a div nested inside an "a" tag (specifically in Link within next.js), there is another div labeled as "like." When clicking anywhere within the main div, the intention is for it to redirect to the destination specified by the "a" tag. However, if th ...

JavaScript's prototype remains unique and is not duplicated

I've encountered an issue where my CoffeeScript classes are not properly copying the prototype. Here is the code snippet I am working with: module.exports = class ListQueue constructor: -> @queue = [] @queueIds =[] @currentId = 0 ...

What alternative can be used instead of Document in Javascript when working with EJS?

Currently, I am utilizing ejs to handle my HTML tasks and I have come across an issue where I cannot use the usual document.getElementById('id') method within this environment. The error message displayed states "document not defined". This has ...