Tharindu
Tharindu

Reputation: 13

How can I access this json data using angular js

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);

            });

Upvotes: 1

Views: 102

Answers (3)

Amila Sampath
Amila Sampath

Reputation: 645

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

Upvotes: 0

Devidas Kadam
Devidas Kadam

Reputation: 944

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);

Upvotes: 2

Amila Sampath
Amila Sampath

Reputation: 645

Modify controller

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

    });

Try it and give console out put

Upvotes: 1

Related Questions