0

I'm new to front end development, apologies if this is not the most sophisticated question. I want to do something fairly simple: display a list of items from the JSON returned by an http call to a REST api. I can't seem to make sense of why my PoC isn't returning anything from the server, not even logging to the console in the callback function in the controller. I based this PoC on the angular-seed project: https://github.com/angular/angular-seed

A sample server response would be a simple array of strings:

["bacon","lettuce","tomato"]

The different bits I setup to get this running are:

ingredient-list.html

<div ng-app="myApp" ng-controller="IngredientListController">
    <input type="text" ng-model="$scope.query">
</div>

<ul>
    <li ng-repeat="ingredient in $scope.ingredients | filter:$scope.query">
        <p>{{ingredient}}</p>
    </li>
</ul>

ingredient-list.js

'use strict';

angular.module('myApp.ingredientList', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/ingredientList', {
    templateUrl: 'ingredient-list/ingredient-list.html',
    controller: 'IngredientListController'
  });
}])

.controller('IngredientListController', function($resource) {
  var Ing = $resource('http://mumbo.com/rest/getIngredients');
  var ingredients = Ing.get();
  ingredients.$promise.then(function(){
    $scope.ingredients = ingredients;
    console.log(ingredients);
  });

});

Debugging has also been a challenge, if you have some suggestions for a rookie please send them over too!

Thanks in advance :)

georgeawg
  • 48,608
  • 13
  • 72
  • 95
kata63
  • 1
  • Don't use `$scope` in HTML templates. – georgeawg Feb 14 '21 at 21:59
  • Use the resource method `query` with arrays. Use `get` with objects. – georgeawg Feb 14 '21 at 22:18
  • The app dependency list should include `ngResource`, e.g. `angular.module('app', ['ngResource']);`. – georgeawg Feb 14 '21 at 22:28
  • The `
      ` element is outside the scope of the `
      ` element on which the controller is instatiated.
    – georgeawg Feb 14 '21 at 22:30
  • Thanks for all those pointers @georgeawg, I applied them back to the code. Replaced $scope with $ctrl in the html template (hopefully that's ok). Changed get() to query(). Added ngResource to the dependency list. Moved the ul element into the div where the controller is instantiated. I also applied@ghchoi 's recommendation and removed the call to $promise. What's weird now is that I'm getting and empty array as the response being logged, but postman returns an array with a bunch of elements in it when I do a GET to the endpoint. – kata63 Feb 14 '21 at 22:42
  • It is important to realize that invoking a `$resource` object method immediately returns an empty reference (object or array depending on `isArray`). Once the data is returned from the server the existing reference is populated with the actual data. For more information, see [AngularJS nrResource API Reference - $resource Service - returns](https://docs.angularjs.org/api/ngResource/service/$resource#$resource-returns). – georgeawg Feb 14 '21 at 22:55
  • So the log isn't written because the response didn't come back in time for it to be displayed? Would this would be different in the browser window, getting populated when the response is received? – kata63 Feb 14 '21 at 22:57
  • The HTML gets updated because the AngularJS framework puts a watcher on the scope variable and updates the HTML when it detects a change. For more information see [How does data binding work in AngularJS?](https://stackoverflow.com/questions/9682092/how-does-data-binding-work-in-angularjs) -- [this answer](https://stackoverflow.com/a/9693933/5535245) by Misko Hevery, one of the creators of AngularJS. – georgeawg Feb 14 '21 at 23:16
  • Awesome, thanks for all the info @georgeawg – kata63 Feb 14 '21 at 23:36

0 Answers0