gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:59:31 25 4
gpt4 key购买 nike

我正在尝试以 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com