I am trying a quick test to debug why some of my code isn't working as expected.
I have a controller named testCtrl
and a service myService
. In the service, I am trying to get data from Parse and once I have the data, I am trying to load this data into my frontend html.
Here's the code:
app.controller('testCtrl', function($scope,myService) {
var currentUser = Parse.User.current();
$scope.username = currentUser.getUsername();
$scope.test = "ths";
var promise1 = myService.getEvaluatorData();
promise1.then(function(response){
$scope.results2 = response;
console.log(response);
});
});
app.factory('myService', function(){
return {
getAllData: function($scope){
getEvaluatorData($scope);
},
getEvaluatorData: function(){
var evaluators = Parse.Object.extend("Evaluators");
query = new Parse.Query(evaluators);
return query.find({
success: function(results){
angular.forEach(results, function(res){
console.log("Looped"); //this is just to verify that the then call below is executed only after all array objects are looped over.
});
} ,
error: function(error){
}
});
}
}
});
Here's the html code where I want to display the data:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body ng-controller="testCtrl">
{{test}}
{{results2}}
</body>
</html>
The results2
don't load up in the html.
Here is the console log.
testParse.js:46 This is done
2015-07-10 15:28:32.539testParse.js:46 This is done
2015-07-10 15:28:32.540testParse.js:46 This is done
2015-07-10 15:28:32.541testParse.js:46 This is done
2015-07-10 15:28:32.542testParse.js:56 Returning result as promised [object Object],[object Object],[object Object],[object Object]
You need to return
data from your service and pass it to your controller. For ex:
app.controller('testCtrl', function ($scope, myService) {
var currentUser = Parse.User.current();
$scope.username = currentUser.getUsername();
$scope.test = "this";
myService.getEvaluatorData().then(function (response) {
console.log('response', response);
$scope.results2 = response;
});
});
app.factory('myService', function ($http, $q) {
return {
getEvaluatorData: function () {
var evaluators = Parse.Object.extend("Evaluators");
var query = new Parse.Query(evaluators);
return query.find({
success: function (results) {
return results;
},
error: function (error) {
return error;
}
});
}
}
});
Also, no point in calling this.getEvaluatorData($scope)
in my opinion, when you can call the getEvaluatorData
method directly