我希望我的json存储在我的控制器中的字符串变量中

时间:2014-10-13 05:17:06

标签: json angularjs pagination

我有一个JSON,我从服务中获得我想使用json存储在我的字符串变量中。 所以我的分页工作。

var app = angular.module('Techapp', ['ngTable']);

app.controller('ngTableCtrl', function ($scope, ngTableParams) {
    var data = [{ Technology: "Core JAVA", CourseType: "Training Plan", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Material", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Plan", JobLevelPrefered: "JL 3+", ContactPerson: "Shanthi_A" },
                { Technology: "Core JAVA", CourseType: "Training Material", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Material", JobLevelPrefered: "JL 3+", ContactPerson: "Muthumari_T" },
                { Technology: "Core JAVA", CourseType: "Checklists", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Plan", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Plan", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Material", JobLevelPrefered: "JL 3+", ContactPerson: "Vatsala_Agrawal" },
                { Technology: "Core JAVA", CourseType: "Training Plan", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Plan", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Material", JobLevelPrefered: "JL 3+", ContactPerson: "Selvam_K01" },
                { Technology: "Core JAVA", CourseType: "Checklists", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" },
                { Technology: "Core JAVA", CourseType: "Training Plan", JobLevelPrefered: "JL 3+", ContactPerson: "Kavitha_arjun" }
               ];

    $scope.tableParams = new ngTableParams(
    {
        page: 1, // show first page
        count: 5 // count per page
    },  
    {
        total: data.length, // length of data
        getData: function($defer, params) {
            $defer.resolve(data.slice((params.page() - 1) * params.count(),  params.page() * params.count()));
        }
    });
});

app.controller('CertificationCtrl', function ($scope, ngTableParams, $http) {
    var url = "http://localhost:58402/Search.svc/abc/ListsJava/Certification";
    $http.get(url)
      .success(function (data) {
         var myjson = JSON.parse(data);
         $scope.Title = JSON.parse(myjson)
      });
    var data = [];
    $scope.tableParams = new ngTableParams(
    {
        page: 1, // show first page
        count: 5 // count per page
    }, 
    {
        total: data.length, // length of data
        getData: function ($defer, params) {
            $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
    });
});
就像在ng-TableCtrl中一样,json是一个字符串变量。所以我的分页工作正常。但是在我的CertificationCtrl中,因为我从服务中获取了json,所以我无法应用分页。请帮忙

由于

喜欢

1 个答案:

答案 0 :(得分:0)

如果http://localhost:58402/Search.svc/abc/ListsJava/Certification已经返回您的JSON数据,那么您不能只在success回调中调用您的分页代码,如下所示:

app.controller('CertificationCtrl', function ($scope, ngTableParams, $http) {
  var url = "http://localhost:58402/Search.svc/abc/ListsJava/Certification";
  $http.get(url)
    .success(function (data) {
      $scope.tableParams = new ngTableParams(
         {
           page: 1, // show first page
           count: 5 // count per page
         }, 
         {
           total: data.length, // length of data
           getData: function ($defer, params) {
             $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
         }
    });
  });
});

<强>更新

请参阅jsfiddle上的现场演示。不要担心Javascript部分中的第1行到第10行;我用它来设置要发送到jsfiddle echo服务的测试数据。将第11行的url变量替换为您的网址。

另请注意,我正在使用$http.post进行演示,并且还删除了所有JSON.parse()代码。