javascript - Angular ng-repeat 不显示来自 Meteor.call 的数据

标签 javascript angularjs meteor angular-meteor

我正在尝试以 ng-repeat 方式将数据获取到 View 。

这是 Controller

class dashboardFaqsCtrl {
  constructor($scope, $reactive) {
    'ngInject';

    $reactive(this).attach($scope);

    this.faqs = [];

    Meteor.call('getFaqs', function (err, res) {
      if (err) {
        console.log(err);
      } else {
        console.log(res);
        this.faqs = [res.data.faq];
        console.log(this.faqs);
      }
    });

  }
}

方法如下

import Future from 'fibers/future';

Meteor.methods({
  getFaqs: function( ) {
    // Create our future instance.
    var future = new Future();

    HTTP.get( 'http://example.com/faq', {}, function( error, response ) {
      if ( error ) {
        future.return( error );
      } else {
        future.return( response );
      }
    });

    return future.wait();
  }
});

View :

<table id="datatable" class="table table-striped table-bordered">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>


  <tbody>
    <tr ng-repeat="item in dashboardFaqs.faqs">
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
    </tr>
  </tbody>
</table>

View 不会重复 ng-repeat。

如何让 Angulars ng-repeat 更新或显示服务器端此 HTTP 调用返回的数据?

谢谢!

回答

由于评论和答案,我能够弄清楚,这是我所做的更改:

class dashboardFaqsCtrl {
  constructor($scope, $reactive) {
    'ngInject';

    $reactive(this).attach($scope);

    this.faqs = [];

    Meteor.call('getFaqs', function (err, res) {
      if (err) {
        console.log(err);
      } else {
        console.log(res);
        this.faqs = res.data.faq;
        $scope.dashboardFaqs.faqs = this.faqs;
        console.log(this.faqs);
        $scope.$apply();
      }
    });

  }
}

最佳答案

您必须在 $scope 中传递数据

class dashboardFaqsCtrl {
  constructor($scope, $reactive) {
    'ngInject';

    $reactive(this).attach($scope);

    this.faqs = [];

    Meteor.call('getFaqs', function (err, res) {
      if (err) {
        console.log(err);
      } else {
        console.log(res);

        this.faqs = [res.data.faq];
        $scope.dashboardFaqs = this.faqs;
        console.log(this.faqs);
      }
    });

  }
}

试试吧!!!

关于javascript - Angular ng-repeat 不显示来自 Meteor.call 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37122091/

相关文章:

javascript - blob 到文件的转换返回 application/octet-stream,而不是 JavaScript 中的 File

javascript - 使用 jQuery 从隐藏字段中的字符串中删除值

javascript - Firestore没有执行操作的权限

javascript - 测试使用 jQuery 的 Angular 指令

meteor - 如何 IF 不在 {{ #each }} 模板内

javascript - react .js :You called `setState` with a callback that isn't callable

javascript - openweathermap API 到 Angular ES6

javascript - Angular 'this' 无法在指令 Controller 内工作

meteor - 在 meteor 中通过LAN开发时如何更改开发环境的ROOT_URL

meteor - 如何允许用户使用浏览器修改服务器上的源代码文件(Meteor应用程序)