A guide on utilizing ng-repeat to iterate through array values in Views

Need help with using ng-repeat on array values within an ng-repeat in VIEWS?

The second ng-repeat is not functioning properly.

Here is the value of generalDocument.documents:

["14-10-2015.xls","15-10-2015.xls","16-10-2015.xls"]

 <div class="box-body table-responsive no-padding">
        <table class="table table-hover table-striped">
          <tr>
            <th>#</th>
            <th>Company</th>
            <th>Branch</th>
            <th>Document Type</th>
            <th>Description</th>
            <th>Reference Number</th>
            <th>Issue Date</th>
            <th>Expiry Date</th>
            <th>Documents</th>
          </tr>
          <tr ng-repeat="generalDocument in generalDocuments | orderBy: 'generalDocument.id' | filter: search">
            <td>{{ $index + 1 }}</td>
            <td>{{ generalDocument.company.company_name }}</td>
            <td>{{ generalDocument.branch.branch_name }}</td>
            <td>{{ generalDocument.document_type.document_type }}</td>
            <td>{{ generalDocument.description }}</td>
            <td>{{ generalDocument.reference_number }}</td>
            <td>{{ generalDocument.issue_date }}</td>
            <td>{{ generalDocument.expiry_date }}</td>
            <td>
                <div class="btn-group">
                  <button type="button" class="btn btn-default  btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                     Download <span class="caret"></span>
                  </button>
                  <ul class="dropdown-menu">
                    <li ng-repeat="document in generalDocument.documents track by $index">
                      <a href="">{{ document }}</a>
                    </li>
                  </ul>
                </div>
            </td>
          </tr>
        </table>
      </div>

UPDATE: After adding track by $index, the loop is working, but it is looping per character.

UPDATE: Upon console.log, generalDocument and generalDocument.document are fetched from the database as:

document: "["EK 845 FDX 20150906 00001 CARGO1.xls","EK 845 FDX 20150906 00004 MOH.xls","EK 847 FDX 20150811 000010 COMMPERSONAL3.xls"]"

Answer №1

Make sure to verify that the "documents" property contains an array instead of a string in your UPDATE statement.

If it appears as a string from the server, be sure to use "JSON.parse" on it before processing. It would be beneficial to update your server response to send the data as an array for consistency.

I hope this information proves useful!

Answer №2

Here is an example that requires the inclusion of angular.min.js:

<html>
    <head>
        <title>Angular JS Directives</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="angular.min.js"></script>
    </head>
    <body>
        <div ng-app="myApp" ng-controller="contrl">
            <p><input type="text" ng-model="city" /></p>
            <p>My City Name is: {{city}}</p>
            <p><input type="number" ng-model="qnty" /></p>
            <p><input type="number" ng-model="cost" /></p>
            <p>Total amount is : {{(qnty * cost)|currency}}</p>
            <p><input type="text" ng-model="test" /></p>
            <ul>
                <li ng-repeat="x in names | filter:test | orderBy:'country'">
                    {{(x.name | uppercase) + ',' + x.country}}
                </li>
            </ul>
        </div>
        <script>
            angular.module('myApp', []).controller('contrl', function ($scope) {
                $scope.qnty = 1;
                $scope.cost = 5;
                $scope.names = [
                    {name: 'Jani', country: 'Norway'},
                    {name: 'Hege', country: 'Sweden'},
                    {name: 'Kai', country: 'Denmark'}];
            });
        </script>
    </body>
</html>

Answer №3

Appreciate everyone's assistance.

The variable generalDocument.document was previously retrieved as a STRING from the database.

I have made changes to my server model so that it now returns a JSON array instead of a string value. This adjustment has allowed the ng-repeat function to operate correctly.

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

Show the total of a JavaScript calculation in a designated input box

Is there a way to show the total sum of this calculation in an input field? function calculateSum1() { var sum = 0; //loop through each textbox and add their values $("input.miles").each(function() { //only add if the value is a number ...

The event "click .className" within the Marionette module is failing to trigger

I'm facing an issue with some code that I've written. Here's a snippet of what it looks like: myApp.module(args, function(args){ Views.MainView = Marionette.ItemView.extend({ //template, tagName, className down: false, events: ...

Getting a string representation of an object from JSON in Swift

My Swift WebSocket connection is returning JSON data structured like this: { "identifier": "{\"channel\":\"SomeChannel\"}", "message": { "resource": { ...

Submitting data with ajax in MVC when an option is chosen in a dropdown menu

Within my form, I have multiple dropdown lists. Whenever a user selects an option from one of these dropdowns, I want that value to be saved in the backend database. To avoid reloading the page, I believe using Ajax is the best approach, but I need assista ...

Searching the JSON file by its value using Waterline

I am struggling with locating model instances based on the nested address attribute in one of my models. attributes: { address: { type: 'json' } } I have attempted various queries to find model instances located in the same city: Model ...

Is it possible to use AngularJS promise scheduling with `async`/`await` syntax?

When working with AngularJS services, TypeScript often recommends that I switch my code to use async/await functions. While I understand that using the await keyword is compatible with third-party promises because it essentially translates to calling then ...

Is there a way to temporarily halt a jQuery animation for 2 seconds before automatically resuming it, without relying on mouse-over or mouse-out triggers?

With just one scrolling image implemented in jQuery, the logos of clients are displayed continuously in a scrolling box with no pauses. Speed can be adjusted easily, but pausing and then resuming the animation after 2 seconds seems to be a challenge whic ...

Removing data with sweetalert in a Ruby on Rails application

Hey there, I'm currently exploring the use of sweet alert js to enhance the appearance of my alert boxes. In my table, I have a specific data deletion feature that is triggered by a standard JavaScript alert confirmation. However, when attempting to i ...

Is there a feature similar to Google/YouTube Insight called "Interactive PNGs"?

Recently, I have been exploring the YouTube Insight feature and I am intrigued by how their chart PNGs are generated. When you view the statistics for a video, the information is presented in interactive PNG images. These images seem to be entirely compos ...

Looping through a multidimensional array in $_POST variables

I have developed a form that sends me multiple values using the post function. The $_POST[groups] contains values 1, 2, 4, and 8 based on 4 different checkboxes. Similarly, $_POST[retailgroup] includes only the values 1, 2, and 4. I want to combine these v ...

The result from the AngularJs promise is coming back as undefined

I am facing an issue while trying to implement the login function of my AuthService factory in conjunction with my AuthLoginController controller. The problem arises when the User.login function is triggered with incorrect email and password details, causi ...

What is the best way to call a JavaScript function within a PHP echo statement that outputs a <div> element?

Having trouble echoing this PHP code due to issues with single quotes, causing the HTML to end prematurely. Any suggestions on how to fix this? function button($conn){ $sql = "SELECT * FROM table"; $result= mysqli_query($conn, $sql); while($r ...

AngularJs: uncomplicated rating system

Is there a way to increase a variable in an ng-repeat loop when clicked? <li class="item" ng-repeat="post in posts"> ... ... <button ng-click="incrementLike(post.like_count)">Like {{post.like_count}}</button> ... ... </li> $scope ...

Modify the background color of checkboxes without including any text labels

I am looking to customize my checkbox. The common method I usually see for customization is as follows: input[type=checkbox] { display: none; } .my_label { display: inline-block; cursor: pointer; font-size: 13px; margin-right: 15px; ...

merging 4 arrays in a specified order, organized by ID

i have below 4 array objects var dataArray1 = [ {'ProjectID': '001', 'Project': 'Main Project 1', 'StartYear': '2023', 'EndYear': '2023', 'StartMonth': 'Sep&apo ...

Assigning an identification number to specify the type of Chip

I am currently working on a project involving Material UI "Chips" that contain text and serve as references. Within the context of my project, I have Chips for both White Advantages and Black Advantages sections. However, there are instances where these Ch ...

What is the significance of the "#" character in the URL of a jQuery mobile

As I encounter a strange issue with my jQuery mobile page, I notice that when accessing page.php everything looks good, but once adding #someDetailsHere to the URL, it only displays a blank white page. What could be causing this and how can I resolve it? ...

Is there a way to identify and retrieve both the initial and final elements in React?

Having an issue with logging in and need to retrieve the first and last element: Below is my array of objects: 0: pointAmountMax: 99999 pointAmountMin: 1075 rateCode: ['INAINOW'] roomPoolCode: "ZZAO" [[Prototype]]: Object 1: pointAmoun ...

Retrieve specialized information from a json file

I have a JSON variable called json which contains some data in JSON format. I am attempting to extract a specific portion of that data. One way to do this is by using the index as demonstrated below: var newdata = json[listid].Taxonomies[0]; However, my ...

javascript date.js parsing and sorting an array of objects

I'm currently working on creating a JavaScript array of objects, utilizing date.js to parse dates, and then sorting that array using date.js compare methods. Below is a snippet of code that highlights the two issues I am facing. Issue 1: How can I ha ...