Jeroen Steen
Jeroen Steen

Reputation: 541

Service for Angular module md-data-table

In the readme of Angular module "md-data-table" it says "Assume we have a $nutrition service". I tried adding a service (makerService), with a return of data. But I can't see the data represented on the screen. I tried different JSON formats, as an error I get:

$makerService.makers.get is not a function

This is my Javascript code:

(function() {

  'use strict';

  angular.module('BowbleCRM', [
    'ngMaterial',
    'md.data.table'
    ])
  .service('$makerService', function () {
    var makers = {
            "makers" : [
                {"full_name": "Jan", "email": "[email protected]"},
                {"full_name": "Pieter", "email": "[email protected]"}
            ]
          };
    return makers;
  })
  .controller('BowbleCRMController', ['$makerService', '$scope', BowbleCRMController]);

  function BowbleCRMController($makerService, $scope) {
    'use strict';

    $scope.currentNav = 'importeren';
      $scope.selected = [];

      $scope.query = {
        order: 'full_name',
        limit: 5,
        page: 1
      };

      function success(makers) {
        console.log(makers);

        $scope.makers = makers;
      }
      $scope.getMakers = function () {
        $scope.promise = $makerService.makers.get($scope.query, success).$promise;
      };
    }

})();

And this is a part of my HTML.

<md-toolbar class="md-table-toolbar md-default">
  <div class="md-toolbar-tools">
    <span>Alle makers</span>
  </div>
</md-toolbar>
<!-- exact table from live demo -->
<md-table-container>
  <table md-table md-row-select multiple ng-model="selected" md-progress="promise">
    <thead md-head md-order="query.order" md-on-reorder="getMakers">
      <tr md-row>
        <th md-column><span>Volledige naam</span></th>
        <th md-column><span>E-Mail adres</span></th>
      </tr>
    </thead>
    <tbody md-body>
      <tr md-row md-select="maker" md-select-id="full_name" md-auto-select ng-repeat="maker in makers.data">
        <td md-cell>{{maker.full_name}}</td>
        <td md-cell>{{maker.email}}</td>
      </tr>
    </tbody>
  </table>
</md-table-container>
<md-table-pagination md-limit="query.limit" md-limit-options="[5, 10, 15]" md-page="query.page" md-total="{{makers.length}}" md-on-paginate="getMakers" md-page-select></md-table-pagination>

How can I make a correct service, and showing the data with the Angular module "md-data-table"?

Upvotes: 0

Views: 302

Answers (1)

lealceldeiro
lealceldeiro

Reputation: 14978

You don't have a function get on $makerService, you just return makers which is an object.

If you want to call a get promise you must implement it on makerService. Something like

 .service('$makerService', function () {
    var makers = {
            "makers" : [
                {"full_name": "Jan", "email": "[email protected]"},
                {"full_name": "Pieter", "email": "[email protected]"}
            ]
          };

    function get(params){
      //..implement your logic here (for your controller code, this should return a promise)
    }
    return {
        makers: makers,
        get: get
    };
  })

And in your controller you can call it like this:

$scope.getMakers = function () {
        $scope.promise = $makerService.get($scope.query, success).$promise;
      };

Upvotes: 1

Related Questions