0

Please advice,

I am creating rest client with json output using angular js , although rest data has received, but it never populated correctly into view html.

is there any wrong $scope data defined at angular controller?

Below is the json rest output data.

{
 "Job": [
    {
        "id": "1",
        "prize": "car"
    }
       ]
}

and the javascript controller with name "some.js"

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

app.controller("PostsCtrl", function($scope, $http) {
$http.get('jsonURL').
success(function(data, status, headers, config) {
  $scope.posts = data.Job;
 }).
error(function(data, status, headers, config) {
 });
});

and html view

<!doctype html>
<html ng-app="MyApp">
<head>
    <title>Test AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
    <script src="some.js"></script>
</head>

<body>
    <div ng-controller="PostsCtrl">
        <ul ng-repeat="post in posts">
            <p>The ID is {{post.id}}</p>
            <p>The prize is {{post.prize}}</p>
        </ul>
    </div>
</body>
</html>
3
  • try upgrading version of angular library, version you have is not very current and goes back to early stages of angular. Also put at least a console.log call in error handler to see if it is firing Commented Sep 17, 2014 at 1:30
  • are you trying to run this from local file? Commented Sep 17, 2014 at 1:34
  • yes.. i run it locally. Commented Sep 17, 2014 at 2:00

1 Answer 1

0

It looks ok to me. One thing you might need to check is that the http response has a content-type header of application/json, otherwise angular will not parse the data correctly.

Try this as a work-around:

var jsonResponse = eval('('+data+')');
$scope.posts = jsonResponse.Job;

Or just make sure your header is set correctly and you shouldn't have to do this.

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

2 Comments

thanks for the idea.. with do modification at object controller and view, it can generate data; $scope.posts = data And at view do delete <ul ng-repeat="post in posts"> and modify <p>The ID is {{post.Job[0].id}}</p>. but this only generate 1st row data only. any idea how to generate all data within ng-repeat function?
I'm not exactly sure what you mean, you're accessing all the data using the 'post' variable generated by the ng-repeat.

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.