【发布时间】:2016-09-04 11:04:32
【问题描述】:
我正在尝试将数据获取到 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。
如何使 angulars ng-repeat 更新或显示从服务器端的这个 HTTP 调用返回的数据?
谢谢!
回答
感谢 cmets 和答案,我能够弄清楚,这就是我所做的更改:
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();
}
});
}
}
【问题讨论】:
-
你需要告诉 Angular 更新范围,查看 $apply
-
@johhan,我认为您不需要 $scope,因为您使用的是 controllerAs 语法。如果你使用 controllerAs 语法,你只想使用 this 而不是 $scope。在 turorial 中查看此页面:angular-meteor.com/tutorials/socially/angular1/…。当然,正如 aw04 所说,您需要 $apply 来告诉 angular 更新绑定。另外,我认为您在“this”中需要 dashboardFaqs,因为您在 ng-repeat 中使用它。
标签: javascript angularjs meteor angular-meteor