madson gr madson gr - 3 months ago 34
AngularJS Question

angularjs + ionic + php mysql (undefined is not a function)

I am trying to load some simple data from db using ionic + angularjs in Intel XDK. I created the theme with http://creator.ionic.io

So, there is a controller.js file controlling every "page". I have a home page and the controller manages it with the code below where I am trying to write the code to bring me the data from the db following http://www.w3schools.com/angular/angular_sql.asp directions.

.controller('homeCtrl', ['$scope', '$stateParams', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $http) {

$http.get("http://localhost/project/select-audiencia.php").then(function(response){

$scope.audiencia = response.data.records;

});

/*$scope.Number = 0;

$scope.test = function(){
return $scope.Number += 1;
}*/

}])

.controller('suporteCtrl', ['$scope', '$stateParams', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams) {


}])


I am getting undefined is not a function in $http.get() line. Php file is fine and returning json code.

$sql = $mysqli->query("SELECT * FROM audiencia");

if($sql->num_rows > 0){
while($row = $sql->fetch_array(MYSQL_BOTH)){
$registro = array(
"ID" => $row['id'],
"VOU" => $row['vou'],
"NAO_VOU" => $row['nao_vou'],
"TALVEZ" => $row['talvez']
);

$retorno[] = $registro;

}
}

$mysqli->close();
$retorno = json_encode($retorno);
echo $retorno;


What is the problem with that function?

UPDATE

<ion-view title="Home" id="page6" style="background-color:#FFFFFF;">
<ion-content padding="true" style="background: url(img/gyZnJFCoRPC41przFivh_bg.png) no-repeat center;background-size:cover;" class="has-header" ng-repeat="x in audiencia">
<div>
<img src="img/XKpA1sK1QMeh5stz2Pe8_logo.png" width="100%" height="auto" style="display: block; margin-left: auto; margin-right: auto;">
</div>
<div id="home-markdown8" style="color:#000000;font-size:16px;text-align:center;">
<p>Comparecerão:
<strong>{{x.VOU}}</strong>
</p>
</div>
<div id="home-button-bar4" class="button-bar margin-top-100">
<button id="home-button12" style="font-style:italic;color:#008BBB;border-radius:30px 30px 30px 30px;" class="button button-light button-block audiencia_btns" ng-click="test()">Vou</button>
<button id="home-button13" style="font-style:italic;color:#008BBB;border-radius:30px 30px 30px 30px;" class="button button-light button-block audiencia_btns">Não Vou</button>
<button id="home-button14" style="font-style:italic;color:#008BBB;border-radius:30px 30px 30px 30px;" class="button button-light button-block audiencia_btns">Talvez</button>
</div>
<div id="home-markdown9" style="color:#000000;text-align:center;">
<p>vao - nao vao - talvez</p>
</div>
<center>
<a ui-sref="menu.temporada2016" id="home-button15" style="font-style:italic;color:#008BBB;font-size:18px;margin-top:50px;border-radius:30px 30px 30px 30px;width:250px;" class="button button-light button-block icon-right ion-chatbubbles pics-news-btn">Fotos e Novidades</a>
</center>
</ion-content>
</ion-view>

Answer

It looks like your problem is that you're not including $http in your controller, try this instead:

....
.controller('homeCtrl', ['$scope', '$http', 
    function ($scope, $http) {
....
Comments