2

I'm going through a few tutorials on consuming an API using AngularJS... I'm running into issues while trying to run {{greeting.id}} and {{greeting.content}}. I'd assume that this renders the results, but they are not visible on my screen.

Here is my CodePen: http://codepen.io/ChaseHardin/pen/bprObb/

  1. https://spring.io/guides/gs/consuming-rest-angularjs/
  2. http://codepen.io/theshravan/pen/mnbcx

Why doesn't my id and content display on the UI; is the issue with my HTML or JavaScript?

HTML

<html>
<head>
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
</head>
<body ng-app="myApp">
  <div ng-controller="GreetingController" class="container">
    <h1>Greeting</h1>
    <hr/>
    <div ng-repeat="greeting in greetings" class="col-md-6">
      <h4>{{greeting.id}}</h4>
      <p>{{greeting.content}}</p>
    </div>
  </div>
</body>
</html>

JavaScript

var myApp = angular.module('myApp', []);

myApp.controller("GreetingController", function ($scope, $http) {
    $http.get('http://rest-service.guides.spring.io/greeting').
    success(function (data) {
        $scope.greeting = data;
    });
});

2 Answers 2

1

Remove the ng-repeat. The results aren't in an array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Michael, that did the trick... I'll mark this as the answer. One last question, would this work the same way if there had been multiple rows of JSON data returned or would I need some ng-repeat?
Yes if the data were in an array the ng-repeat would have worked perfectly. You can test this in the codepen, I've forked it and made the results an array... codepen.io/mkl/pen/vGJvLv
1

Here you go;

var myApp = angular.module('myApp', []);

myApp.controller("GreetingController", function ($scope, $http) {
    $http.get('http://rest-service.guides.spring.io/greeting').then(function (data) {
      $scope.greetings = data;
      console.log(data);
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.