AngularJS ng-repeat - http.gs retrieving data, but not displaying

孤街浪徒 提交于 2019-12-11 11:53:50

问题


I am new to angular and trying to integrate it within my application. I am attempting to use a simple $http.get to a .JSON file, which displaying the matching contents in a ng-repeat

Here my get:

$scope.countries = [];
$http.get('/resources/data/countries-report.json').success(function(data) {
    $scope.countries = data.countries;
    // alert(JSON.stringify($scope.countries));
    console.log(data.countries);
    console.log(data.countries.population);
}).error(function(error) {
    alert('error');
});

Here's my .JSON file:

{
  "firstName"   : "Joe",
  "surName" : "Bloggs",
  "countries" : [
      { "name": "France", "population": "63.1" },
      { "name": "Span", "population": "52.3" },
      { "name": "United Kingdom", "population": "61.8" }
  ]
}

Here is my HTML:

<li ng-repeat="country in countries">
    {{country.name}} has population of {{country.population}}
</li>

When viewing in the browser, all that is displayed is:

  • has population of
  • has population of
  • has population of

It seems as though my code can see there are 3 countries, as when i add or remove from my .JSON file, the list in the HTML modifies accordingly, however, the contents of the .JSON is not displaying.

Have i forgot to return the data from my .get??

** UPDATE *************************

As some have mentioned, my code seems to be correct, i think i know what the problem may be.

My application makes use of a HTML templating structure using Swig which accesses .JSON file using {{ }}.. Could this be causing confusion with Angular?

** UPDATE *************************

If i change:

var app = angular.module("app", []);

to

var app = angular.module('app', []).config(function($interpolateProvider){
        $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
    }
);

And:

<li ng-repeat="country in countries">
     {{country.name}} has population of {{country.population}}
</li>

To:

<li ng-repeat="country in countries">
     {[{country.name}]} has population of {[{country.population}]}
</li>

Then the correct values are displayed.


回答1:


you have to get the data in the array of object form like

[
  { 
    id:1,
    ima:gh.jpg,
    data: 
    [
      {
        anotherid:1,
        my:w,
        ki:y
      }
    ]
  },
  {
    id=2,
    ima:jh.jpg
  }
]


来源:https://stackoverflow.com/questions/23214048/angularjs-ng-repeat-http-gs-retrieving-data-but-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!