【问题标题】:AngularJS read data from controller to tableAngularJS 从控制器读取数据到表
【发布时间】:2018-05-05 09:47:02
【问题描述】:

我只是尝试从 balanceTableCtr 中读取数据并将其显示在 balanceTable 中。但它只是向我展示了一张空桌子。

balanceTableCtr.js:

(function () {
    'use strict';

    angular.module('BlurAdmin.pages.dashboard')
        .controller('BalanceTableCtrl', BalanceTableCtrl);

    /** @ngInject */
    function BalanceTableCtrl($scope) {
        $scope.balanceTableData = [
            {
              image: 'app/browsers/chrome.svg',
              algorithm: 'SHA-256',
              name: 'Bitcoin',
              price: '9843 $',
              change: '12.6 %',
              isChangeUp: true,
              amount: '2.452 BTC'
            }
          ];
    }
})();

balanceTable.directive.js:

(function () {
  'use strict';

  angular.module('BlurAdmin.pages.dashboard')
      .directive('balanceTable', balanceTable);

  /** @ngInject */
  function balanceTable() {
    return {
      restrict: 'E',
      controller: 'BalanceTableCtrl',
      templateUrl: 'app/pages/dashboard/balanceTable/balanceTable.html'
    };
  }
})();

balanceTable.html:

<div class="horizontal-scroll">
  <table class="table table-hover">
    <thead>
    <tr class="black-muted-bg">
      <th class="align-right">Algorithm</th>
      <th class="align-right">Name</th>
      <th class="align-right">Price</th>
      <th class="align-right">Change 24h</th>
      <th class="align-right">Amount</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="item in balanceTableData" class="no-top-border">
      <td class="align-right">{{item.algorithm}}</td>
      <td class="align-right">{{item.name}}</td>
      <td class="align-right">{{item.price}}</td>
      <td class="align-right">{{item.change}}</td>
      <td class="align-right">{{item.amount}}</td>
    </tr>
    </tbody>
  </table>
</div>

dashboard.html:

<div class="row">
  <div class="col-lg-6 col-md-12">
    <div ba-panel ba-panel-title="Hover Rows" ba-panel-class="with-scroll table-panel">
      <div include-with-scope="app/pages/dashboard/balanceTable/balanceTable.html"></div>
    </div>
  </div>
</div>

我对 Angular 1 很陌生。但我在 Internet 上四处查看,他们都是这样做的。为什么它不起作用?

【问题讨论】:

  • .directive('balanceTable', balanceTable); 更改为.directive('balanceTable', dashboardLineChart);
  • 我改了,但是表还是空的:(
  • 控制台有什么?有什么错误吗?
  • 你为什么不用&lt;balance-table&gt;
  • 没有没有错误

标签: angularjs angularjs-directive angular-datatables angular-controller


【解决方案1】:

Check this plunkr

<div class="row">
<div class="col-lg-6 col-md-12">
  <div ba-panel ba-panel-title="Hover Rows" ba-panel-class="with-scroll table-panel">
    <balance-table> </balance-table> 
  </div>
</div>

【讨论】:

    【解决方案2】:

    非常感谢。 解决方案是我必须在 tbody 标签中添加 ng-controller。

    <tbody ng-controller="BalanceTableCtrl">
        <tr ng-repeat="item in balanceTableData" class="no-top-border">
          <td class="align-right">{{item.algorithm}}</td>
          <td class="align-right">{{item.name}}</td>
          <td class="align-right">{{item.price}}</td>
          <td class="align-right">{{item.change}}</td>
          <td class="align-right">{{item.amount}}</td>
        </tr>
    </tbod>
    

    【讨论】:

    • 但是你为什么要将它创建为指令?您正在编写的代码很糟糕。你把事情搞混了。
    • 我真的不知道我需要什么指令?我坚持将控制器连接到 html... 你能给我解释一下吗?
    • 指令用于 DOM 操作。如果您只需要显示数据,请转到 ng-include 或者如果您正在使用版本 > 1.5 并且您需要重用它,那么请使用组件。不要对directive 和与controller 关联的单独template 使用相同的template。查看 AngularJS 教程以获得更好的理解
    猜你喜欢
    • 2017-12-30
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多