Working with arrays in AngularJS using JavaScript

Hey there, I need some assistance. How do I assign the result from a GET method to the $scope.chat variable? Thanks in advance.

app.controller('ChatCtrl', ["$scope","$http", function ($scope,$http) {
 $http({
            method: 'GET',
            url: 'assets/controller/chat/messages.php'
                 }).then(
                    function(response1){
                        $scope.contents=response1.records;


     });

$scope.selfIdUser = 50223456;
$scope.otherIdUser = 50223457;
$scope.setOtherId = function (value) {
    $scope.otherIdUser = value;
};

var exampleDate = new Date().setTime(new Date().getTime() - 240000 * 60);                    

**$scope.chat = [{
    "user": "bok",
    "content": "test1",
    "idUser": 1,
    "idOther": 2
},{
    "user": "bok",
    "content": "test2",
    "idUser": 1,
    "idOther": 2
}];**   

$scope.sendMessage = function () {
    var newMessage = {
        "user": "Peter Clark",
        "avatar": "assets/images/avatar-1.jpg",
        "date": new Date(),
        "content": $scope.chatMessage,
        "idUser": $scope.selfIdUser,
        "idOther": $scope.otherIdUser
    };
    $scope.chat.push(newMessage);
    console.log(newMessage);
    $scope.chatMessage = '';

};
}]);

Answer №1

Alright!

Here is the code snippet:

$http({
            method: 'GET',
            url: 'assets/controller/chat/messages.php'
                 }).then(
                    function successCallback(response){
                    alert(response);// shows [Object, Object]?
                    console.log(response);// Shows multiple values?
                    alert(response[0][0]);// should display 'test'
                    alert(response[0]['content']);// same or different
                        $scope.contents=response.records;
     });

The console outputs a significant amount of data, indicating that the information is present.

I apologize for the back-and-forth questions, but I need to test it out. Could you create a fiddle?

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 deduct pixels from numbers using JavaScript?

Currently, I am attempting to adjust the height of the footer based on the height of another div element. My approach involves utilizing the .css("height") function. However, I am encountering difficulty as the function does not seem to return the value i ...

Search for text within the nearest <p> tag using HTML and jQuery

My table contains different td elements with the same ID, as shown in this foreach loop: <td class="AMLLeft" style="display:inline-block; !important">ID: <p class="important">${item.id}</p> </td> <td align="right" nowrap="tr ...

Generating a fresh instance of a class that mirrors an already existing instance, all without relying on eval()

I have an object named uniqueObject of an unspecified class and I am in need of creating a duplicate object from the same class. Here's my current approach: I extract the class name using uniqueObject.constructor.name. Then, I generate a new object o ...

Having trouble sending an ajax request from localhost to a remote server

When attempting to make an ajax request (using jquery) from my local server to a remote page where I am the administrator, I encounter the following error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin &ap ...

SetBootstrapTooltip at the location of the mouse pointer

Is it feasible to implement a bootstrap tooltip that follows the mouse cursor in AngularJS? If not, what other options are available for achieving this functionality? ...

I am unable to find any resolution, so to speak

Despite reading numerous posts and trying different examples on my own, I still can't grasp this particular question that has been asked many times before. My struggle lies in returning images from various folders and processing them individually in a ...

Working with JSON in AJAX with Javascript and C# to handle array data

When attempting to send an array via AJAX using JSON, I am encountering a problem where my C# handler is unable to properly handle the query. It seems that the querystrings are merging together inexplicably. In the scenario presented here, I am trying to ...

The system encountered an error when attempting to convert the data '19-10-2002' into a date format

I am trying to pass a date parameter in the format (dd-MM-yyyy) and then convert it into the format (yyyy-MM-dd) before sending it via API. Here is my code: convert(date:string){ date //is in the format(dd-MM-yyyy) date = formatDate(date , " ...

Determine the height of an image using JavaScript

How can I retrieve the height of an image in a JavaScript function? When I use the code: var image_height = $(image).height(); The value of image_height is 0, even though my image definitely has non-zero height. Is there a different method to accurately ...

Bug Found in AngularJS v1.3.15: Text Color Rendering Glitch on Page Load with WebKit

It appears that the text colors (which should be blue) are not displaying correctly until a user hovers over the text or resizes the window. I attempted to resolve this issue by adjusting the transition property so that it triggers on hover/active states ...

Utilizing locally hosted JavaScript in an HTML document

Hi everyone, I am an extreme beginner in HTML and JavaScript so I would appreciate explanations to be as simple as possible. Despite reading various versions of this problem, I am still struggling to figure it out. Any help is greatly welcomed. My goal is ...

Converting a JSON Model object to HttpServletRequest in a legacy app using Angular JS and Spring REST

I am faced with the task of working on a large enterprise legacy Java application that relies heavily on servlets and JSPs. My plan is to modernize this outdated app by converting it into a Single Page application using Angular JS and Spring MVC REST. As ...

When attempting to embed Ruby code within JavaScript, Ruby is not being acknowledged or

I am facing an issue with accessing values of a hash created by a ruby function inside javascript. This is the code snippet from my controller: class TransferController < ApplicationController def index require 'json' #@t ...

Bizarre Actions of a JavaScript Object

Currently, I am in the process of developing a Multiplayer game using Phaser and Eureca io. My main focus right now is on perfecting the authentication of players and their controls. To achieve this, I have implemented a method on the server that retrieves ...

Using JSDoc and Visual Studio Code: Writing documentation for a function that is being passed as an argument to another

Struggling to properly document the input parameters for a JavaScript function using JSDoc. The documentation suggests using the @callback comment, but Visual Studio Code (VSCode) doesn't seem to recognize it. In VSCode, the intellisense for the loca ...

Ways to alter the default background color in material-ui

I am currently working on creating a dashboard for my company and I am looking to modify the default styles of some material components. Specifically, I want to change the background color of the Paper component without having to add my style to each ind ...

A perpetual JavaScript countdown that constantly loops with a delayed response

I'm currently working on an online auction bidding project that involves users placing bids before time runs out. I found a solution on this link. My goal is to run the timer loop continuously. When the timer hits 0, I want the bid button to be disab ...

Steps to assign a value to an input element using the state in a React application

Hey there, I hope everything is going well for you! I have a question regarding setting the value of an input field based on the state received from props. I've tried to populate the input with data from the "profile" state using placeholders, but it ...

The animation for the accordion feature in the iOS Framework7-vue app seems to be moving at

I am encountering issues with the iOS app while developing both android and iOS apps with Framework7-vue. The Android app functions smoothly, but the iOS app is causing trouble. One of the features include a popup functionality with an accordion inside fo ...

How can we increase the accuracy of numerical values in PHP?

When performing a math operation in JavaScript, the result is: 1913397 / 13.054 = 146575.53240386088 However, when the same operation is performed in PHP, the result is slightly different: 1913397 / 13.054 = 146575.53240386 This discrepancy may be due ...