Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
    
setup a fiddle. –  Matt Way 2 days ago
    
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 –  charlietfl 2 days ago
    
are you trying to run this from local file? –  charlietfl 2 days ago
    
yes.. i run it locally. –  hadi 2 days ago

1 Answer 1

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.

share|improve this answer
    
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? –  hadi yesterday
    
I'm not exactly sure what you mean, you're accessing all the data using the 'post' variable generated by the ng-repeat. –  snowman4415 yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.