kumara
kumara

Reputation: 937

loading data using ng-table issue

I am trying to display data set using, ng-table. but data is not loading. please check my sample service and controller

app.service('ContactService', function () {
    var bfLosses = [ 
        { idx: 0, uid: 'User 11', name: 'Name 11', area: 'Area 1'},
        { idx: 1, uid: 'User 12', name: 'Name 12', area: 'Area 1'},
        { idx: 2, uid: 'User 21', name: 'Name 21', area: 'Area 2'},
        { idx: 3, uid: 'User 22', name: 'Name 22', area: 'Area 2'}
    ];    
}

i need to access above array to populate table. my controller

app.controller("businessController", function ($scope,NgTableParams,ContactService){
    $scope.tableParams = new NgTableParams({
        group: "area"
      },{
        dataset:   $scope.bfLosses
    });    
});

when define array inside the controller , working well. but my requirement is accessing it inside the controller.

Upvotes: 0

Views: 44

Answers (2)

3960278
3960278

Reputation: 796

You can also use 'this' in case of service not in case of factories

app.service('ContactService', function () {
this.bfLosses = [ 
    { idx: 0, uid: 'User 11', name: 'Name 11', area: 'Area 1'},
    { idx: 1, uid: 'User 12', name: 'Name 12', area: 'Area 1'},
    { idx: 2, uid: 'User 21', name: 'Name 21', area: 'Area 2'},
    { idx: 3, uid: 'User 22', name: 'Name 22', area: 'Area 2'}
  ];
});

Controller:

app.controller("businessController", function ($scope,NgTableParams,ContactService){
  $scope.tableParams = new NgTableParams({
    group: "area"
  },{
    dataset:   ContactService.bfLosses
  });
});

Upvotes: 0

Gordon Mohrin
Gordon Mohrin

Reputation: 609

Service should return reference:

app.service('ContactService', function () {
    var bfLosses = [ 
        { idx: 0, uid: 'User 11', name: 'Name 11', area: 'Area 1'},
        { idx: 1, uid: 'User 12', name: 'Name 12', area: 'Area 1'},
        { idx: 2, uid: 'User 21', name: 'Name 21', area: 'Area 2'},
        { idx: 3, uid: 'User 22', name: 'Name 22', area: 'Area 2'}
      ];
    return {
      bfLosses: bfLosses 
    };
}

Service should be accessed by the dependency you injected:

app.controller("businessController", function ($scope,NgTableParams,ContactService){
    $scope.tableParams = new NgTableParams({
        group: "area"
      },{
        dataset:   ContactService.bfLosses
      });
});

Upvotes: 0

Related Questions