组件中的Ng-repeat无法显示控制器中$ http.then收费的数据。我做错了什么?
app.js中的服务调用rest方法:
app.service('dataService', dataService);
function dataService ($http) {
return {
loadData: loadData
}
function loadData() {
var promise = $http.get("http://example.com/channels").then(function (response) {
res = response.data;
});
return promise;
}
在component.js中加载数据
function channelListController(dataService){
this.channels = [];
/** If i create var like this, this works, but i want load from http method:
this.channels = [{id:1, name: "xxxxx", description: "xxxxxxx", sport: "xxxx"}]**/
dataService.loadData().then(function(d) {
console.log(d); //This console.log is showing DATA correctly!
this.channels = d;
});
尝试在视图中显示数据:
<tr ng-repeat="channel in $ctrl.channels">
<td>{{ channel.name }}</td>
<td>{{ channel.sport }}</td>
(...)
(使用http-server进行部署)
频道未在视野中显示。如果我使用名为channels的数据创建一个var,在catchListController之前捕获http.method,通道显示OK,但是如果我将数据收费到&#34; .then&#34;在控制器中,我无法显示任何内容。跟踪进入.then,http get方法正确接收数据,但我无法将数据加载到通道中。变种
谢谢!
答案 0 :(得分:0)
看起来你的引用是错误的。以下应该解决它
function channelListController(dataService){
var that = this;
this.channels = [];
/** If i create var like this, this works, but i want load from http method:
this.channels = [{id:1, name: "xxxxx", description: "xxxxxxx", sport: "xxxx"}]**/
dataService.loadData().then(function(d) {
console.log(d); //This console.log is showing DATA correctly!
that.channels = d; // use that instead of this
});