Андрей Гузюк Андрей Гузюк - 4 months ago 14
Node.js Question

$http.get in Angular doesn't work

I have problem with

$http.get
when I am trying to get data from my DB ( mongoDB).

I'm using mean stack.

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
});


In addition, I have one more question:

When I'm using
controllerAs
in component, my browser says


Uncaught ReferenceError: Wildrose is not defined


But, if remove it it would be OK.

I have tried solving it as follows:

Just add in the constructor a little bit code:

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);
});;
}
}

Answer

ControllerAs requires string and not an object:

controllerAs: "wildrose"

Have you set up an endpoint at your server with node/express under /api/wildrose that returns a json string read from your database?

Please provide more code from your back-end and errors, otherwise it is hard to diagnose.