Javascript 无法将promise数据绑定到angularjs中查看
我已经尝试了stackoverflow上的例子,但在我看来仍然一无所获。我可以在控制台中看到articleIds,但在视图中什么也看不到。提前谢谢你的帮助 这是我的控制器:Javascript 无法将promise数据绑定到angularjs中查看,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,我已经尝试了stackoverflow上的例子,但在我看来仍然一无所获。我可以在控制台中看到articleIds,但在视图中什么也看不到。提前谢谢你的帮助 这是我的控制器: export class MainController { constructor ($http) { 'ngInject'; this.$http= $http; this.apiHost = 'http://localhost:5005/api/articles'
export class MainController {
constructor ($http) {
'ngInject';
this.$http= $http;
this.apiHost = 'http://localhost:5005/api/articles';
this.getArticles();
}
getArticles() {
var vm = this;
this.$http.get(this.apiHost).then(function(result){
vm.articles = result.data;
vm.articles.forEach(function(item){
console.log(item.articleId);
});
});
}
}
以下是我的看法:
<div class="form-group">
<label for="resumeId">Select article:</label>
<select class="form-control" id="resumeId">
<option ng-repeat="item in main.vm.articles" value="{{item.articleId}}">{{item.articleId }}</option>
</select></div>
这与承诺无关,HTML只是引用了错误的范围。在$http回调中,您将结果分配给vm.articles,其中vm是控制器的作用域,因此可以在main.articles而不是main.vm.articles中使用articles
在main.articles中将您的ng repeat更改为ng repeat=item,它将起作用。我添加main.vm.articles,因为路由使用controllerAs:“main”您在articles中尝试过ng repeat=item吗?据我所知,这应该行得通。非常感谢!ng repeat=main.articles工作中的项为了将此问题标记为已解决,我将添加此项作为答案。如果答案对您有效,请随意批准。不是vm==这个,只需将main.articles中的代码更改为ng repeat=item我试图解释的是vm是控制器的作用域。已更新问题以改进此问题。