Storing information within AngularJS

As a newcomer to the world of coding and Angular, I am currently working on developing a calculator-style web application that includes a rating section in the footer. My main concern revolves around saving data so that it can be accessed by other users. The specific portion of code related to this is outlined below:

.controller('rateController', function() {
      this.positive = 0;
      this.negative = 0;
      this.addLike = function () {
          this.positive = this.positive + 1;
          this.liked = true;
      };
      this.addDislike = function () {
          this.negative = this.negative + 1;
          this.disliked = true;
      };
  });
      <div>
          <table style="text-align:center; font-size:1em" class="table-responsive table" ng-controller="rateController as rating">
              <tr>
                  <td>I liked it!</td>
                  <td>I didn't like it!</td>
              </tr>
              <tr>
                  <td><p class="likes" ng-click="rating.addLike()">+{{rating.positive}}</p></td>
                  <td><p class="dislikes" ng-click="rating.addDislike()">-{{rating.negative}}</p></td>
              </tr>
          </table>
      </div>

I would greatly appreciate any guidance on how to save the number of likes and dislikes to the website's server. If you wish to see the current functionality, please visit this link. The rating table can be found in the footer.

Answer №1

Consider looking into the AngularJS $http or $resource libraries as they can be very useful for making server calls.

One helpful tip is to start your controller by assigning 'this' to a named variable to avoid any issues if 'this' gets reassigned within certain functions.

    $http.post('/someUrl', data, config)
          .then(function () {
               alert('success'); 
           }), function() {
               alert('something went wrong'); }
            );

Answer №2

As someone who has been eager to store data on a server but lacked the means to do so, I believe PHP is the ideal solution. By creating a PHP script, you can facilitate the process of upvoting and downvoting specific items based on the URL parameters passed through (for example,

http://example.com/vote.php?vote=up&page=help
). This will enable the saving of votes either to a file (which requires a basic understanding) or a database (which demands a deeper knowledge of PHP). Subsequently, you can make AJAX requests to this PHP script, which can then provide you with the total number of votes received thus far.

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

Is there a way to efficiently use AJAX and PHP to iterate through JSON data and save it to a new JSON file

I have created a simple movie editor with a sidebar where users can drag and drop elements. On the right side, users can edit these elements as they wish and save the data to a JSON file. When the user clicks save, I want it to create a new file and save t ...

The ng-repeat function is unable to fetch data from a JSON file

Within my AngularJS framework template, I have the following snippet of html code: <ul class="sb-parentlist"> <h1 class={{headerClass}}> Popular</h1><div data-ng-repeat="data in data track by $index"> <li> ...

How can I export an ES Object in Node.JS to an NPM package?

I am currently facing a challenge involving an ES5 object/function that I want to integrate into an NPM package. This particular object is within a namespace defined as MY_NAMESPACE.myObject = function(){...} where MY_NAMESPACE is simply an object. Typic ...

When I try to run Parcel, my ReactJS website just won't deploy in a serverless environment

For a while now, I've been working on a website using serverless. Everything was going smoothly until this morning when I encountered an issue with pushing updates from my site to the serverless platform. When trying to push, I received the following ...

Is it possible to transfer data from javascript to php through ajax?

I am attempting to extract the data speedMbps from my JavaScript code using Ajax to send the data to my PHP script, but unfortunately, I am not receiving any output. My experience with Ajax is limited to implementing auto-completion feature. <script sr ...

Filter an array using regular expressions in JavaScript

Currently, I am facing a challenge in creating a new array of strings by filtering another array for a specific pattern. For example: let originalString = "4162416245/OG74656489/OG465477378/NW4124124124/NW41246654" I believe this pattern can be ...

Retrieve modified fields

Working with a ui-grid and making some edits to it, resulting in changes being made to the binded data within the grid. What would be the best approach for pushing this updated data back to the server? I believe the optimal solution would involve sending ...

Challenge: RxJS timeout function not functioning as expected

I am facing an issue with exiting the Observable stream after 3 seconds if there is no new string input. The problem arises when I paste the same value multiple times, as the distinctUntilChanged operator prevents the input stream from progressing. I wan ...

Error: NextJS cannot access the 'map' property of an undefined object

Hey everyone, I usually don't ask for help here but I'm really struggling with this piece of code. I've tried looking at various resources online but can't seem to figure out why it's not working. Any guidance would be greatly appr ...

The HTML generated by Selenium using Javascript is still missing some elements, despite accessing the document.body.innerHTML

Attempting to retrieve the HTML from a webpage that undergoes modification by JavaScript post-loading. Followed directions in this guide, executing the command below in my Python script after initial page load: html = browser.execute_script("return docume ...

Manipulate values within an array when a checkbox is selected or deselected

I am developing an Angular application where I have created a checkbox that captures the change event and adds the checked value to an array. The issue I am facing is that even if the checkbox is unchecked, the object is still being added to the array. D ...

What are the steps to compile Sencha Touch using sencha-touch.jsb3?

I've been working on modifying the bundled sencha-touch.jsb3 file in an effort to decrease the size of the framework code. Here's what I've done so far: First, I downloaded the Sencha SDK Tools from I then made edits to SenchaTouch/sen ...

What is the method to invoke a function within a factory in angularjs by using a string parameter?

I have a complex logic that I want to encapsulate in an AngularJS factory for easy use with dependency injection. The challenge is that the logic is dynamic, so I don't know in advance what functions will be available. What I have is a string represen ...

Create and export a React component with dual properties

Currently, I am utilizing MaterialUI in my project and exporting components in the following manner: import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles"; ... export default withStyles(styles)(Users); Recently, I have integrated ...

Turn off choices by utilizing data type attribute values within select2 version 4

I'm attempting to deactivate the options by using the data-type attribute specified for each option in select2. Unfortunately, my attempts have been unsuccessful thus far. In addition, I am encountering this error within the change event handler: ...

Utilize Ionic and Angular to display the local storage value within a text box

I am in the process of developing an application using the Ionic framework. I am able to upload files to the server and receive a response, which I then save the file name in localstorage. However, I am facing an issue where I need to display this value in ...

Learn how to dynamically import and load components in VueJS while rendering a list

I am currently rendering a list. <div v-for="msg in messages"> <div v-if="msg.type==='component'"> <component v-bind:is="getComponent(msg.component)" :data="msg.data" :text="msg.text"></component> </div> ...

Displaying a loading indicator while a file is downloading and the page is being

Is there a way to show a loading indicator while a PDF is being generated in PHP? I redirect to a separate page for the PDF generation process, but the original page stays open and simply downloads the file once it's ready. How can I make a loading in ...

Ensuring security: Email confirmation and password recovery using Django REST framework and AngularJS

Seeking guidance on how to utilize the built-in views for password reset in django.rest.auth and establish an email verification system for registration using the django rest framework and angularjs. I've scoured the web for tutorials or comprehensiv ...

The absence of an 'Access-Control-Allow-Origin' header on the requested resource prohibits access from the origin 'localhost:8080'

I have encountered this problem multiple times on various forums, but so far none of the suggested solutions have worked for me. Currently, I am developing a MEAN stack application that utilizes PassportJS for Twitter login functionality. angular.module( ...