1

Im trying to access this json data using ng-repeat in angular. But I data does not come to the view.

<div class="panel-body" ng-repeat="product in Brands track by $index">

                        <label>{{product.ID}}</label>
                        <label style="margin-right: 10px;">{{product.Name}}</label>
                        <input type="checkbox" ng-model="formData.Selected" ng-true-value='"Y"'
                               ng-false-value='"N"' value="{{product.Name}}" style="margin-right: 10px;" cd-true-value="'{{product.ID}}'" cd-false-value="'None'" />

This is a part of json data

Object {SelectFromDate: Thu Apr 07 2016 00:00:00 GMT+0530 , ToDate: Fri Apr 15 2016 00:00:00 GMT+0530 , Selected: "1"}
    {"data":[{"ID":1,"BrandID":1,"ProductID":1,"Name":"***","Created":"****"},{"ID":2,"BrandID":2,"ProductID":1,"Name":"***","Created":"****"},{"ID":3,"BrandID":3,"ProductID":1,"Name":"***","Created":"****"},{"ID":4,"BrandID":4,"ProductID":1,"Name":"***","Created":"****""},    {"ID":5,"BrandID":5,"ProductID":1,"Name":"***","Created":"****"},

this is the controller

$scope.submit = function() {
        console.log($scope.formData);


            angularService.PostAngularFormData($scope.formData).then(function (response) {
                $scope.Brands = JSON.stringify(response);
                console.log($scope.Brands);

            });
5
  • Can you provide " console.log(response) " console log out put ? Commented Apr 19, 2016 at 10:31
  • the part I have shown as json data is the console.log responce Commented Apr 19, 2016 at 10:34
  • Can you provide " console.log(response) " console log all out put ? I try my self Commented Apr 19, 2016 at 11:13
  • when I try to log responce this error occurs Unexpected token o at Object.parse (native) Commented Apr 19, 2016 at 11:17
  • @Amila Thank you so much for your support. When I used angular.fromJson instel of JSON.stringify it worked. Thanks again for the help Commented Apr 19, 2016 at 11:34

3 Answers 3

2

Use this

$scope.allResponse = JSON.parse(response);
$scope.brands = $scope.allResponse.data;
console.log($scope.brands);

or use

$scope.brands = angular.fromJson(response.data);
console.log($scope.brands);
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for the reply. But when I use this I get the following error 'angular.js:13294 SyntaxError: Unexpected token o at Object.parse (native)
Can you provide " console.log(response) " console log out put ?
the part I have shown as json data is the console.log responce
you have misspelled the 'Brands' is 'Brandss'
if you wants to products then use $scope.allResponse = JSON.parse(response); $scope.products = $scope.allResponse.data; you will get an array of products in $scope.products. use it in ng-repeat
|
1

Modify controller

angularService.PostAngularFormData($scope.formData).then(function (response) {
        $scope.Brands = response.data; 
        console.log($scope.Brands);

    });

Try it and give console out put

11 Comments

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object] 0 : Object 1 : Object 2 : Object 3 : Object 4 : Object 5 : Object 6 : Object 7 : Object 8 : Object 9 : Object 10 : Object 11 : Object 12 : Object 13 : Object 14 : Object 15 : Object 16 : Object 17 : Object 18 : Object 19 : Object 20 : Object'
I have been using ng-repeat to get data
ng-repeat="brand in Brands track by $index" <-- use and get result
When I use that I only get a large blank space in the view. No text or data visible
If ng-repeat="brand in Brands" and inside ng-repeat div use {{brand}}, What is view out come?
|
0

Modify controller

// I assume you result array 
var array = {"data":[{"ID":1,"BrandID":1,"ProductID":1,"Name":"RI","Created":"2016-04-11T23:‌​19:03.463"},
                 {"ID":2,"BrandID":2,"ProductID":1,"Name":"BLE","Created":"2016-04-11T‌​23:19:03.48"} ]};

$scope.brands = array.data;

Inside view

<div ng-repeat="brand in brands">
    {{brand}}
</div>

my View out put

{"ID":1,"BrandID":1,"ProductID":1,"Name":"RI","Created":"2016-04-11T23:‌​19:03.463"}
{"ID":2,"BrandID":2,"ProductID":1,"Name":"BLE","Created":"2016-04-11T‌​23:19:03.48"}

Try it and solve your problem

1 Comment

Thanks @Amila. I will try this too. angular.fromJson solved this too. Thanks again

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.