Hey there,
i have big issues with my php backend. I want to load the mysql data trough my php Backend in a factory. But the output is blank. To first i know that the backend works, because if i make the $http.get request in the controller all works fine. Here is some code:
Part of my php Backend:
if($db_connection) {
$utfQuery = "SET NAMES 'utf8'";
mysqli_query($db_connection, $utfQuery);
$query = "SELECT * FROM testDB";
$result = mysqli_query($db_connection, $query);
$data = array();
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
}
Part of my controller.js:
.controller('TestCtrl', function($scope, $ionicModal, $http, $ionicPopup, $ionicLoading, Tests) {
$scope.tests = Tests.all();
console.log($scope.tests);
})
Part of my service.js:
.factory('Tests', function($http) {
var tests = [];
return {
all: function() {
$http.get("http://www.******.com/getTestBackend.php")
.success(function(response) {
tests = response.data;
return tests;
})
.error(function(response) {
alert("Something happened");
});
}
}
})
Part of my Html Code:
<ion-item class="item item-icon-left item-icon-right item-remove-animate" ng-repeat="item in tests track by $index" href="#/tab/tests/{{item.id}}">
<i class="icon ion-social-{{item.os}}"></i>
<h2>{{item.title}}</h2>
<p>asked by {{item.user}} ({{item.date}})</p>
<i class="icon ion-chevron-right icon-accessory"></i>
</ion-item>
ADB Log output:
D/SystemWebChromeClient(10117): file:///android_asset/www/js/controllers.js: Line 5 : undefined
I/Web Console(10117): undefined:5
In case that i make the request trough the controller all works fine but i want to organise it as factory for a better code and more possibilitys. I think the Problem is that the factory only get the data as object instead of an array. How to solve a problem like that?