How would I access this JSON data in AngularJS?

后端 未结 2 448
余生分开走
余生分开走 2021-01-29 01:26

I am trying to learn how to load JSON with Angular. I\'m a beginner with both topics, and am stuck on some really beginner topics.

I have a JSON file called food.json. I

相关标签:
2条回答
  • 2021-01-29 01:46

    The first problem is in the controller definition: You are trying to reference $http but you are not passing it in.

    myApp.controller('EmailsCtrl', ['$scope', '$http', function ($scope, $http) {

    Then on success your data parameter is your foodlist:

    .success(function (data, status, headers, config) { $scope.foodlist = data; })

    Finally, in your html, ng-repeat should be like this:

    ng-repeat="food in foodlist"

    0 讨论(0)
  • 2021-01-29 01:57

    ng-repeat in your case will iterate the array of objects in your data

    You just need to use :

    <li ng-repeat="food in foodlist">
    

    Then on each pass of the loop, angular will reference the current object in array to produce output like {{ food.title }}

    I updated your demo here

    Edit Strongly suggest you go through the tutorial on angular docs site step by step

    0 讨论(0)
提交回复
热议问题