How to parse JSON in an Angular JavaScript file

Below is the JSON data I am working with:

[
    {
        "name": "1QQQJohnQQQ11_12_1998",
        "age" : "ads"
    },
    {
        "name": "2QQQEvaQQQ05_11_1989",
        "age" : "ads"
    },
    {
        "name": "3QQQCasperQQQ12_06_1994",
        "age" : "ads"
    },
    {
        "name": "4QQQBeanQQQ30_12_1996",
        "age" : "ads"
    }]

Next, here's a snippet of my JavaScript file:

var app = angular.module('app', []);
        app.service('service', function($http, $q){
            var deferred = $q.defer();

            $http.get("datesss.json").then(function(data){
                deferred.resolve(data);
            });

            this.getNames = function(){
                return deferred.promise;
            }
        });
        app.controller('secondCtrl', function($scope, service){
            var promise = service.getNames();
            promise.then(function(data){
                $scope.names = data.data;
                var namesplit = $scope.names
                namesplit.map(function(item) {
                    item.type = item.name.split('QQQ')[0];
                    item.date = item.name.split('QQQ')[1];
                    item.name = item.name.split('QQQ')[2];
                });
                console.log(namesplit);
                });
    });

I needed to split the name field from the JSON by the delimiter "QQQ" in the JavaScript file. When I log namesplit, everything seems fine.

Now, my goal is to display "type", "date", and "name" in a table. I attempted the following code:

<thead>
                        <tr>
                            <th class="text-center">type</th>
                            <th class="text-center">date</th>
                            <th class="text-center">name</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="name in namesplit">
                            <td>{{name.type}}</td>
                            <td>{{name.date}}</td>
                            <td>{{name.name}}</td>
                        </tr>
                    </tbody>

Unfortunately, it didn't work as expected. Can someone please provide guidance? Thank you in advance.

Answer №1

Replace every instance of namesplit with $scope.namesplit within your controller.

In this case, be aware that there is no scope variable named namesplit in the statement ng-repeat="name in namesplit".

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

Click on the ng-click attribute to access the HTML page

I need a button that can perform the following tasks: Execute multiple functions from the controller using ng-click(they must be called in HTML) Direct to another html page located in "static/page.html" onclick, when used for navigation (location.hr ...

When Vue is used to hide or show elements, MDL styles may be inadvertently removed

When an element is hidden and shown using Vue, some MDL styles may disappear. Take a look at this example on CodePen: https://codepen.io/anon/pen/RBojxP HTML: <!-- MDL --> <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=M ...

Retrieve JSON information from External document through Ajax

I'm trying to incorporate an external JS file that contains some sample JSON code. However, I'm encountering an error when I include the sample JSON code in the file, specifically at the ":". Interestingly, when I validate the code using online t ...

Rerender not occurring after array splice with React hooks setter

My parent component structure is as follows: import React from "react"; import Test from "./Test"; function App() { const [configs, setConfigs] = React.useState([1, 2, 3]) return ( <div> ...

How to access a TypeScript global variable from outside an Angular controller?

In my main.ts file, there is a global variable named rowTag which is an array of Tag[] entities. Additionally, I have an angular controller named TagMeController.ts. Below is the constructor of TagMeController: constructor($scope, $rootScope) { ...

Discovering whether an ID exists within a variable containing HTML code

I am currently attempting to determine if the ID is included in a variable containing HTML content. The ID name is being added to a DIV element through dynamic variables. strHTML = "<div id='"+var1+var2+"'>" Now, I want to verify if a sp ...

The mesmerizing world of parallax animations and the smooth scrolling experience

I recently built a website using the SUPERSCROLLORAMA plugin, only to discover later that there are issues with parallax scrolling on iPad and iPhone. Now I'm trying to figure out how to solve this problem. It seems that events are disabled on these ...

Filling in form fields with data from the database based on the option selected from the dropdown menu

I'm trying to create a dynamic form where selecting a name from a drop-down menu will automatically populate the rest of the fields with that person's information without refreshing the page. I believe I need to use an onChange function in JavaSc ...

Incorporate a tooltip into a horizontal bar chart created using D3

Having trouble adding a tooltip popup to display the year and value when hovering over the bar. Tried multiple options without success. Any suggestions? Experimented with a similar approach as shown in this link but still facing issues, especially with th ...

A guide on expanding an HTML table dynamically with MVC razor syntax

My goal is to dynamically add new rows to a table by following the advice given in this answer on Stack Overflow: Add table row in jQuery I have successfully implemented it for one of my table requirements as seen below: function onAddItem() { $( ...

retrieve today's date with parsed time using moment

I am attempting to retrieve the current date and time, with the specified time using moment js. Here's what I have tried. const time = '18:00' const timeAndDate = moment(time) However, when I display timeAndDate, it indicates an invalid da ...

Switch Object to WebElement or SearchContext in JavaScript

Looking for the best way to convert an Object to WebElement or SearchContext using JavaScript? In Java, casting as `(WebElement)` or `(SearchContext)` works fine. However, attempting the same in JavaScript with `as Webelement` or `as SearchContext` result ...

Javascript Calculator with Dual Input Fields

I have been given a task to create a calculator by tomorrow using Javascript. Unfortunately, I am currently taking a distance course and Javascript has just been introduced in this assignment. While I am familiar with HTML and CSS, Javascript is something ...

Is it possible to utilize Angular's dependency injection in place of RequireJS?

As a newcomer to Angular, I am wondering how I can organize my code into separate files without using requirejs or any other framework. After watching a brief introductory video, it seemed possible. Currently, my app looks like this and functions well: v ...

Focus on an empty <input> tag with just the type attribute

In what way can React Testing Library be utilized to isolate a blank <input> element that solely possesses a type attribute? For instance, consider an input field that will eventually have attributes added dynamically, much like the surrounding labe ...

Encountered a runtime error in Xcode Swift: Thread 1 terminated due to signal SIGABRT while attempting to display

I need assistance with two issues. 1. I am consistently receiving a signal SIGABRT error and cannot pinpoint the cause. The suggested solutions have not been successful. Project can be found below! 2. After retrieving data from my website, how can I effect ...

How is it possible that this event listener is able to capture an event that was already sent before it was

I am facing an issue with my Vue JS code. When I click on the account <a> tag, it triggers the toggle method. The toggle method adds an event listener to the document. However, as soon as the toggle method is executed, the event listener fires and ...

Is it possible to conceal the contents of a details tag without using a summary tag?

I'm looking for a way to hide the details tag without the summary. In my code, the summary is only visible when a condition [isvisible == false] is met. However, even when the summary is not visible, the details keyword is still shown and I want to hi ...

Utilizing AngularJS to dynamically inject HTML content into $scope

In my possession are the following files: index.html //includes instructions for passing arguments to the btnClick function in app.js <div ng-bind-html="currentDisplay"></div> app.js app.factory('oneFac', function ($http){ var htm ...

Leverage the power of AJAX for fetching data from a database in Node.js

As I am utilizing Node.js as the server and Mysql as the database to store logged in usernames, I am curious if it is possible to use AJAX to execute a SQL database query and retrieve a list of usernames. More precisely, I am interested in using XMLHttp r ...