Angular模块md-data-table的服务

时间:2017-03-10 15:16:50

标签: javascript angularjs material-design

在Angular模块的自述文件中" md-data-table"它说"假设我们有一个$营养服务"。我尝试添加一个服务(makerService),返回数据。但我无法看到屏幕上显示的数据。我尝试了不同的JSON格式,因为我得到了一个错误:

$makerService.makers.get is not a function

这是我的Javascript代码:

(function() {

  'use strict';

  angular.module('BowbleCRM', [
    'ngMaterial',
    'md.data.table'
    ])
  .service('$makerService', function () {
    var makers = {
            "makers" : [
                {"full_name": "Jan", "email": "jan@test.nl"},
                {"full_name": "Pieter", "email": "pieter@test.nl"}
            ]
          };
    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;
      };
    }

})();

这是我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>

如何制作正确的服务,并使用Angular模块显示数据&#34; md-data-table&#34;?

1 个答案:

答案 0 :(得分:1)

您在$makerService上没有makers功能,只需返回get作为对象。

如果您想致电makerService承诺,则必须在 .service('$makerService', function () { var makers = { "makers" : [ {"full_name": "Jan", "email": "jan@test.nl"}, {"full_name": "Pieter", "email": "pieter@test.nl"} ] }; function get(params){ //..implement your logic here (for your controller code, this should return a promise) } return { makers: makers, get: get }; }) 上实施。像

这样的东西
$scope.getMakers = function () {
        $scope.promise = $makerService.get($scope.query, success).$promise;
      };

在您的控制器中,您可以这样称呼它:

self.commentTable.delegate = self
self.commentTable.dataSource = self