-1

Still fairly new to angular and am seeing some weird behavior from doing an ng-repeat.

When I run the following code, I get 1410 rows back rendered as part of the repeat. I only have 5 rows coming back from the search. None of the data-binding works. No idea where I’m getting the extra 1405 rows wither. I have no idea why Angular seems to be inserting 1405 rows though I assume it's something obvious I'm just overlooking.

UPDATE The reason I'm getting ~1400 rows back is because the ng-repeat is treating $scope.results as if it were a string so it's giving me a row per char. Still not sure why it's doing that however.

I have the following code:

     <table class="table">
             <thead>
                 <tr class="filters">
                     <th class="filter-col">Preview</th>
                     <th class="filter-col">Content Id</th>
                     <th class="filter-col">Doc Type</th>
                     <th class="filter-col">Account Number</th>
                     <th class="filter-col">End Date</th>
                 </tr>
             </thead>
             <tbody >
                 <tr ng-repeat="row in results track by $index">
                     <td><a href="">View</a></td>
                     <td>{{row.document_Id}}</td>
                     <td>{{row.document_type_2}} + "\" + {{row.document_type_3}} + "\" + {{row.document_type_4}} + "\" + {{row.document_type_5}}</td>
                     <td>{{row.account_number}}</td>
                     <td>{{row.end_datee}}</td>
                 </tr>
             </tbody>
         </table>

I have the following data in scope

 results: [
     {
         "document_Id": 66539,
         "document_type_2": "Deposit",
         "document_type_3": "Deposit Documents",
         "document_type_4": "Deposit Documents",
         "document_type_5": "Photo ID",
         "end_date": "2008-01-07T00:00:00",
         "account_number": 57860.0
     },
     {
         "document_Id": 66536,
         "document_type_2": "Deposit",
         "document_type_3": "Deposit Documents",
         "document_type_4": "Deposit Documents",
         "document_type_5": "W-9 Request for Taxpayer ID",
         "end_date": "2008-01-07T00:00:00",
         "account_number": 57860.0
     },
     {
         "document_Id": 66530,
         "document_type_2": "Deposit",
         "document_type_3": "Signature Cards",
         "document_type_4": "Signature Cards",
         "document_type_5": "Signature Card-Single",
         "end_date": "2008-01-07T00:00:00",
         "account_number": 57860.0
     },
     {
         "document_Id": 66523,
         "document_type_2": "Deposit",
         "document_type_3": "Deposit Documents",
         "document_type_4": "Deposit Documents",
         "document_type_5": "ATM Card Request",
         "end_date": "2008-01-07T00:00:00",
         "account_number": 57860.0
     },
     {
         "document_Id": 66522,
         "document_type_2": "Deposit",
         "document_type_3": "Deposit Documents",
         "document_type_4": "Deposit Documents",
         "document_type_5": "Boarding Data Account Summary",
         "end_date": "2008-01-07T00:00:00",
         "account_number": 57860.0
     }
]

UPDATE: Here's the controller:

app.controller('SearchController', function ($scope, $http, LoginStateService, TokenStateService) { $scope.stateService = LoginStateService; $scope.tokenService = TokenStateService;

    //Search Types
    $scope.searchTypes = [{ text: "Documents", value: "document" }, { text: "Items", value: "item" }, { text: "Reports", value: "report" }, { text: "Statements", value: "statement" }];
    $scope.selectedSearchType = $scope.searchTypes[0];

    //Search Terms
    $scope.searchTerms = [{ text: "Account Number", value: "account_number_f" }, { text: "Amount", value: "amount" }, { text: "Doc Type Path", value: "document_type_path" }, { text: "Date", value: "end_date" }, ]
    $scope.selectedSearchTerm = $scope.searchTerms[0]; 

    //Rows to Return 
    $scope.rowsPerPage = [{ text: "5", value: 5 }, { text: "10", value: 10 }, { text: "15", value: 15 }, { text: "20", value: 20 }, { text: "40", value: 40 }, ];
    $scope.selectedRowsPerPage = $scope.rowsPerPage[3];

    $scope.showSearchResults = false;
    this.logoImage = "images/ActiveView_ContentMgm_140x40.png";

    this.doSearch = function ()
    {
        var loginUrl = "http://ten1.com/services/rest/demoservice.svc/Search";

        delete $http.defaults.headers.common['X-Requested-With'];

        $http({
            method: 'GET', url: loginUrl, params: {
                authenticationToken: $scope.tokenService.getAuthenticationToken(), searchType: $scope.selectedSearchType.value,
                searchTerm: $scope.selectedSearchTerm.value, searchValue: this.searchValue, rowsToReturn: $scope.selectedRowsPerPage.value
            }
        })
        .success(function (data, status, headers, config)
        {
            $scope.status = status;
            $scope.results = JSON.parse(data);

            //$scope.gridOptions = { data: 'results' };

            $scope.showSearchResults = true;
        })
        .error(function (data, status, headers, config)
        {
            $scope.results = data || "Search Request failed";
            $scope.status = status;

            alert("Search Request Failed");
        });
    };

});
Lukasz Koziara
  • 4,274
  • 5
  • 32
  • 43
  • document_Id is with a capital I – Chancho Jun 16 '14 at 19:26
  • I've made a fiddle with your code and except the type mentionned by @Chancho and the missing **]** in your array which, I hope, is a typo; your issue can't be reproduced http://jsfiddle.net/kp8Qd/ – Julien Jun 16 '14 at 19:32
  • good catch with the capital "I", though that did not fix the problem. Also, the object array does end with a "]" I just missed it when copying and pasting it. – Brian Flynn just now edit – Brian Flynn Jun 16 '14 at 19:43
  • The issue is with your JSON being invalid. There are roughly 1400 characters (depends how whitespace is counted) so it looks like it is being interpreted as a string. "results" should be quoted as well. Might help to show how that is being used in the controller. – lucuma Jun 16 '14 at 19:50
  • I've updated to include the controller. – Brian Flynn Jun 16 '14 at 20:00
  • @lucuma You're wrong, you should read this question carefully: http://stackoverflow.com/questions/1262376/is-there-a-limit-on-how-much-json-can-hold – Julien Jun 16 '14 at 20:00
  • lucuma was right, it is treating $scope.results as a string.....each row represents a char. The question is, why is it doing that? – Brian Flynn Jun 16 '14 at 20:08
  • @BrianFlynn remove the parse and see if it works. – lucuma Jun 16 '14 at 20:31
  • Removing the parse yielded the same results as with just the JSON.parse, it was treated as a string. So far, only the double parse seems to do the trick. – Brian Flynn Jun 17 '14 at 12:11

2 Answers2

0

So in the end, the problem was this line:

      $scope.results = JSON.parse(data);

It parsed the results of my service to a string. I changed the line to :

        $scope.results = jQuery.parseJSON(JSON.parse(data)); 

And that fixed it. Surely there's a better way to get an object without a double parse?

-1

i see 2 problems:

  1. document_Id is with a capital I
  2. Your object array is not correct, should be like this: (missing a ] in the end
$scope.results: [
              {
                  "document_Id": 66539,
                  "document_type_2": "Deposit",
                  "document_type_3": "Deposit Documents",
                  "document_type_4": "Deposit Documents",
                  "document_type_5": "Photo ID",
                  "end_date": "2008-01-07T00:00:00",
                  "account_number": 57860.0
              },
              {
                  "document_Id": 66536,
                  "document_type_2": "Deposit",
                  "document_type_3": "Deposit Documents",
                  "document_type_4": "Deposit Documents",
                  "document_type_5": "W-9 Request for Taxpayer ID",
                  "end_date": "2008-01-07T00:00:00",
                  "account_number": 57860.0
              },
              {
                  "document_Id": 66530,
                  "document_type_2": "Deposit",
                  "document_type_3": "Signature Cards",
                  "document_type_4": "Signature Cards",
                  "document_type_5": "Signature Card-Single",
                  "end_date": "2008-01-07T00:00:00",
                  "account_number": 57860.0
              },
              {
                  "document_Id": 66523,
                  "document_type_2": "Deposit",
                  "document_type_3": "Deposit Documents",
                  "document_type_4": "Deposit Documents",
                  "document_type_5": "ATM Card Request",
                  "end_date": "2008-01-07T00:00:00",
                  "account_number": 57860.0
              },
              {
                  "document_Id": 66522,
                  "document_type_2": "Deposit",
                  "document_type_3": "Deposit Documents",
                  "document_type_4": "Deposit Documents",
                  "document_type_5": "Boarding Data Account Summary",
                  "end_date": "2008-01-07T00:00:00",
                  "account_number": 57860.0
              }
            ]
Taifun
  • 6,165
  • 17
  • 60
  • 188
Chancho
  • 1,930
  • 2
  • 15
  • 20