I am building an app using Node JS and Angular JS. I got the data as response from the Back end. But failed to show the data in UI.
my code is like below,
var app = angular.module('app', [])
app.controller('appCtrl', ['$scope', '$http', 'storeData', function ($scope, $http, storeData) {
$scope.searched = []
$scope.locationSearch = function () {
google.maps.event.addDomListener(window, 'load', function () {
// get data from Google Api
$scope.places = new google.maps.places.Autocomplete(document.getElementById('searchPlaces'));
google.maps.event.addListener($scope.places, 'place_changed', function () {
// set the corresponding field
$scope.place = $scope.places.getPlace();
$scope.address = $scope.place.formatted_address;
$scope.latitude = $scope.place.geometry.location.lat();
$scope.longitude = $scope.place.geometry.location.lng();
$scope.mesg = "Address: " + $scope.address + "\nLatitude: " + $scope.latitude + "\nLongitude: " + $scope.longitude;
// call the service
$scope.searched = storeData.putData({ "userId": '6235', "place": $scope.address, "latitude": $scope.latitude, "longitude": $scope.longitude })
})
})
}
}])
app.service('storeData', ['$http', function ($http) {
// this.searchResults = []
this.putData = function (place) {
// call the back end
return new Promise(function (resolve, reject) {
$http({
method: 'POST',
url: '/store',
headers: 'Content-Type: application/json',
data: place
})
}).then(function (success) {
console.log(success.data)
// this.searchResults = success.data
return success.data
}).catch(function (err) {
console.log('unable to process request')
})
}
}])
I am using Google Location api to get a place search box. All the data is ok. They are sent to the back end correctly. And response is coming good.
I have used Chrome Developer tools to check the response. Response it Good. But I am unable to show it in the UI.
My HTML code is like below,
<table class="table" ng-controller="appCtrl">
<tr ng-repeat="result in searched">
<td>{{result.place}} </td>
<td>{{result.latitude}} </td>
<td>{{result.longitude}} </td>
</tr>
</table>
Somehow I have to manage the $scope.searched
to show the data. Can you please tell what is wrong in the code ?? And how to correct it??
I want to use the Service to call the back end and send the output to the Controller.