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

时间:2016-05-09 17:39:39

标签: javascript angularjs meteor angular-meteor

我试图在ng-repeat中获取数据。

这是控制器

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();
  }
});

观点:

<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>

视图不会重复ng-repeat。

如何从服务器端进行角度重复更新或显示从此HTTP调用返回的数据?

谢谢!

ANSWER

由于评论和答案,我能够弄明白,这是我改变的内容:

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();
      }
    });

  }
}

1 个答案:

答案 0 :(得分:1)

您必须在$ 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);
      }
    });

  }
}

试一试!!!