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

What is the best way to set up a dropdown menu in a data grid where the width matches the fields?

I am looking to optimize the layout for various screen resolutions and ensure compatibility with the latest versions of IE, Firefox, and Chrome. ...

Unexpected behavior encountered while serializing a Dictionary using JsonConvert

Hey there! I have a Dictionary<string,Dictionary<CustomClass,string>> that needs to be serialized. The desired output should look like this: { "key1":{ { "CustomClassProperty1":"val1", "CustomClassProperty2" ...

Sharing data between controllers using factory in AngularJS is not supported

I attempted to share data between two controllers by using a factory. However, it seems that the data is not being shared properly between the two inputs. In the AppCtrl controller, I set Data.FirstName to be equal to lattitude. But when I switch over to ...

Open a JavaScript file to retrieve data from a nearby JSON object

I've been trying to access a local JSON file using a JavaScript file, but for some reason it's not working. Even though I'm sure the URL is correct, the code keeps failing to retrieve data from the JSON object. JavaScript file: var pieData ...

Copying arrays in C/C++ that are members of structs by value

Let's explore this C++ struct: typedef struct Person { std::string name; std::string children[MAX_NUM_CHILDREN]; } Person; In the scenario where: void do_something(const Person& p) { Person person_copy = p; } We are wondering wheth ...

Having trouble getting rid of the border-bottom?

I have been attempting to customize the appearance of the React Material UI tabs in order to achieve a design similar to this: https://i.stack.imgur.com/tBS1K.png My efforts involved setting box-shadow for the selected tab and removing the bottom border. ...

When a user clicks on an anchor tag, the corresponding value of the checked item is then returned

I have 3 sets of radio buttons. When a specific anchor with the "round" class is clicked, two actions should occur: The associated set of radio buttons needs to become visible The value of the checked input for that element should be returned. I am ...

Check out this stylish Twitter-inspired SVG text counter created with a combination of CSS and jQuery! See it in action here: https://jsfiddle.net/moss24

Looking to develop a text counter similar to Twitter that increases the width in green color up to 75%, then switches to yellow until 98%, and finally turns red if the input value is greater than or equal to 400. It should also add a "highlight" class when ...

Using Javascript Regular Expressions to validate that the first number in an amount is non-zero

Need a solution to only accept numbers that do not start with zero, but can contain zeros after the first digit. Currently using var.replace(/[^1-9]/g, ''); which prevents input of 0 altogether. Examples of valid inputs: 10 9990 Examples of in ...

Utilizing the spread syntax for elimination

I want to remove a key. Check this out console.log(state); When I do, I get {1: {here is next object}}, next const { 1: deletedValue, ...newState } = state; console.log(newState); console.log(state); But then I end up with {1: {here is next object}} ...

What causes the .getJSON function to return a MIME type error when trying to access the API

I've been attempting to make a call to the Forismatic API, but I keep encountering a MIME type error when sending it. JQuery Request: $(document).ready(function() { $("#quote-button").on("click", function(){ $.getJSON("https://api.forism ...

Using AngularJS to interact with neighboring DOM elements

Imagine a scenario where there is a div containing 5 img elements. When one of these img elements is hovered over, the goal is to change the class of all the elements on its left side. <div id="stars"> <img src="star.png" data-rating="1" ng- ...

The system encountered an error while attempting to access the property "getChild" of an unspecified object

I am currently developing a react application and encountering an issue when trying to call a function from the render method. The function I'm trying to call utilizes the getChild method, but I keep receiving an error stating "cannot read property &a ...

Error in defining class variable within the constructor not found

Just started delving into CoffeeScript and facing a challenge. In my code snippet below, I initialize WebSocketServer with a number as the first argument and a function as the second argument. However, when the websocket receives a message, @msgHandler sud ...

Using Regular Expressions to Strip Quotation Marks from Numerical Values within a String in ColdFusion Version 9

When referring to a numeric value in the given JSON, it should not be within double quotes. I've come up with a temporary solution for this issue, but having a more generic REReplace() function that can be reused would be incredibly helpful. For exam ...

Encountering a problem with Node.js because of a SyntaxError: Receiving an

Recently, I decided to dive into learning node.js and attempted something very basic: displaying a "Hello World" message from the server. The code snippet I used (taken directly from a book) is as follows: var http = require("http"); http.createServer(fu ...

How can we address the tagging directive problem of binding tags as an array of JSON objects and ensuring that tagging functions properly with the ng-keypress event?

Check out this plnkr example. I'm having trouble binding my tags list with ng-model. I want to bind the tags as an array of objects on a function call using ng-keypress, and then post it to a json file in a specific format. Here is what I am looking f ...

creating an interactive table with the help of the useState hook

I'm new to JavaScipt and ReactJS, so I have a question that may seem obvious but I can't seem to figure it out. I am attempting to display my array in a table using useState, but currently I can only show the header. Additionally, if anyone know ...

Tips for dynamically updating an input within a shadow DOM using code?

Snippet: https://jsfiddle.net/5zrwdjy7/2/ I'm currently working on automating keystrokes with a script within Cypress, however, the target website I am dealing with utilizes web components as inputs. These input elements are nested inside what is kno ...

How can I delay the loading of a link until the pop-up is closed?

I have successfully implemented a pop-up on my website, but I am facing an issue where I need to prevent any linked pages from loading until the visitor clicks on the accept button. However, I am struggling to make it function as intended. Below is the sn ...