【发布时间】:2016-11-11 18:54:25
【问题描述】:
当我尝试从我的数据库 (mongoDB) 获取数据时,$http.get 出现问题。
我正在使用平均堆栈。
angular.module('wildroseApp')
.controller('WildroseComponent', function($scope, $http){
$http.get('/api/wildrose')
.success(function (data) {
$scope.wildrose = data;
console.log($scope.wildrose);
})
.error(function (err) {
alert('Error!');
});
})
.component('wildrose', {
templateUrl: 'app/wildrose/wildrose.html',
controller: WildroseComponent,
controllerAs: Wildrose
});
另外,我还有一个问题:
当我在组件中使用controllerAs 时,我的浏览器显示
未捕获的 ReferenceError: Wildrose 未定义
但是,如果删除它就可以了。
我尝试如下解决:
只需在构造函数中添加一点代码:
class WildroseComponent {
constructor($http) {
this.$http = $http;
this.wildrose = [];
}
$onInit(){
this.$http.get('/api/wildrose')
.then(response => {
this.wildrose = response.data;
console.log(this.wildrose);
});;
}
}
【问题讨论】:
-
你使用的是哪个版本的 AngularJs?
-
您的问题/问题是什么? “不起作用”到底是什么意思?
-
当我使用 $http.get 从我的数据库中获取数据时,console.log($scope.wildrose) 中没有任何显示,但这里应该是 json 字符串
-
请尝试将您的 AngularJs 更新到 1.5.7,HTH ;)。
标签: angularjs node.js mongodb express yeoman