Twisted reality
Twisted reality

Reputation: 540

get value from json to angularjs object

I want to get value from this JSON response into angular js object.

[{"label":"Test","value":""," Test2":"","required":"mandatory","type":"text","typeemail":""}]

into angularjs object, here is .html

<tr ng-repeat="item in searched = (coba | filter:customerid | filter:search ) | coba:(currentPage-1)*itemsPerPage |limitTo:viewby ">
    <td>
      <p>{{item.additionaldata_pay}}</p>
    </td>
</tr>

here is .js file

app.controller("cobacont", ['$scope','$http',function($scope,$http) {
      $scope.cari = function () {
        $http.get('...a='+$scope.month).then(function(response){
          $scope.coba = response.data; 
        });
      }  
}]);

Upvotes: 0

Views: 102

Answers (2)

Rajasimman R
Rajasimman R

Reputation: 495

var app = angular.module('main-App', []);
app.controller('customersCtrl', function($scope,$http) {

$scope.records = [];

$http.get("https://...a='+$scope.month")
  .then(function (response) {
     console.log(response.data);
     $scope.records = response.data;
  });
});

here is my html table

<tr dir-paginate="value in data | itemsPerPage:5" total-items="totalItems"  ng-repeat="user in records">
    <td>{{$index+1}}</td>
    <td >{{user.label}}</td>
    <td >{{user.value}}</td>
    <td>{{user.required}}</td>
    <td>
        <button data-toggle="modal" data-target="#edit-data" class="btn btn-primary" ng-click="selectedUser(user)">Edit</button>
        <button ng-click="DeleteUser(user)" class="btn btn-danger" data-toggle="modal" data-target="#delete-data">Delete</button>
    </td>
</tr>

Upvotes: 1

Sulaiman Adeeyo
Sulaiman Adeeyo

Reputation: 495

It's best that you put all data processing in your controller. And leaving the HTML to display your data only.

So, in your controller, add another method that will do your search, filter, and pagination, and the result can then be used in your view.

$scope.getData = function () {
   var data = $filter('filter')($scope.coba, $scope.searchQuery);
   // do pagination here...
   return data;
}

Your HTML can then become something like this:

<tr ng-repeat="item in getData()">
    <td>
        <p ng-bind="item.additionaldata_pay"></p>
    </td>
</tr>

Upvotes: 0

Related Questions