Problems with $http and ng-repeat Beginner Angularjs $$hashKey

Asked

Viewed 621 times

4

Hello I’m a beginner in Angular and I’m finding it very cool, my problem is this:

I have an ng-repeat that works fine when I get the data source of a javascript variable like this:

var alertsq = [  
{  
    "alert":"mediun",
    "nt":"28",
    "nu":"28",
    "no":"34",
    "dtini":"2012/Jul/23",
    "no":"3",
    "dtoc":"23/7/2012",
    "dtuo":"25/7/2012",
    "id":"227529436529033216",
    "msg":"Uh oh, this could be bad. Check the door lock vendor before you book you next hotel room: http://t.co/n56emZf2"
},
{  
    "alert":"mediun",
    "nt":"28",
    "nu":"28",
    "no":"34",
    "dtini":"2012/Jul/23",
    "no":"3",
    "dtoc":"23/7/2012",
    "dtuo":"25/7/2012",
    "id":"227529436529033216",
    "msg":"Uh oh, this could be bad. Check the door lock vendor before you book you next hotel room: http://t.co/n56emZf2"
}];

My controller that picks the variable alertsq and arrow in scope is like this:

app.controller("alertsController", function(){    
    console.log(alertsq);
    this.alerts = alertsq;
}); 

The cool thing is that it works and my list on *ng-repeat* is filled beautifully, but when I use the $http to load a JSON content from a file the same does not fill the list: ó controller code is like this:

app.controller("alertsController", function($http,$scope){    
    $http({
        url: "data/alerts.json",
        dataType: "json",
        method: "GET",
        headers: {
          "Content-Type": "application/json"
        } 
    }).success(function(data){
        $scope.alerts = data;
        console.log($scope.alerts);
    }).error(function(error){
        console.log(data);
        console.log(error);
    });
}); 

The cool thing is that JSON is coming right out of the browser in the first case at the list is thus filled in:

mainController controller.js:7

 [Object, Object, Object, Object, Object, Object, Object]
     0: Object
     $$hashKey:"object:4" 
     alert: "mediun"
     dtini: "2012/Jul/23"
     dtoc: "23/7/2012"    
     dtuo: "25/7/2012"
     id: "227529436529033216"
     msg: "Uh oh, this could be bad. Check the              
     door lock vendor before you book you next hotel room:       
     http://t.co/n56emZf2"
     no:"3"    
     nt: "28"
     nu: "28"__proto__: 
    Object1: 
    Object2:

And this is the console output when I search for $http JSON:

 [Object, Object, Object, Object, Object, Object, Object]
 0: Object 
 alert: "mediun"
 dtini: "2012/Jul/23"
 dtoc: "23/7/2012"    
 dtuo: "25/7/2012"
 id: "227529436529033216"
 msg: "Uh oh, this could be bad. Check the              
 door lock vendor before you book you next hotel room:       
 http://t.co/n56emZf2"
 no:"3"    
 nt: "28"
 nu: "28"__proto__: 
Object1: 
Object2:

The detail is that in the output obtained by JSON through $http there is no attribute $$hashKey, and so the list in ng-repeat is not populated :(, can anyone help me solve this?

Thank you for your attention!

  • The json request is ok, probably the error is in the html iteration. See your source running normally http://plnkr.co/edit/Y36tkeVAIPEFp22WnLqi?p=preview

  • 2

    Hello the solution to my problem here:
 [Problems with ng-repeat][1]
 
 
 [1]: http://stackoverflow.com/questions/27390959/dificult-reading-json-and-add-ng-repeat-list-list-is-not-completed-http-has/393640#27393640

  • Failed to put ng-repeat html

  • If you post your HTML already would help... because the problem as quoted is your HTML not your http request, controller, or anything...

  • @Wagnermonteverde So answer yourself with the link pro Soen, but translate the paragraphs of the answer so that it is not flagged as "just a link"

2 answers

1

Hello, I had problems with json bringing javascript objects, ng-repeat cannot work with objects, I had to use a filter object2Array.

ng-repeat="item in items | object2Array "

minhaApp.filter('object2Array', function(){
    return function(input){
        return angular.fromJson(input);//out;
    }
});

0

Come on, things I found strange:

}).success(function(data){
        $scope.alerts = data;
        console.log($scope.alerts);
    }

You are returning a JSON so the default format of its arrival is:

{index:valor}

In attributing that datathat you are attributing to $scope.alerts should be a data.index, why the date ta te returning a set of objects and not an arrayList

Well, I don’t know if that might be the mistake, but if you want to try it, it’s tip :D

NOTE: Instead of setting $http use the presets already set by the angular itself! Example:

$http.get('linkdarequisição')
    .success(function(data){
       //codigo
    })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.