2016-10-01 16 views
-1

MySQL 데이터베이스에서 PHP 파일로 데이터를로드하고 그 결과를 anglejs 파일에로드하려고합니다. 그러나 작동하지 않습니다. .get() 함수를 사용하여 $ http 서비스로 시도했다.

의 config.php :

<?php 
define('HOST','localhost'); 
define('NAME','test'); 
define('USER','root'); 
define('PW',''); 

$mysqli = new mysqli(HOST,USER,PW,NAME); 
if(mysqli_connect_errno()) 
{ 
//echo("Failed to connect, the error message is: ".mysqli_connect_error()); 
exit(); 
} 
else 
{ 
//echo "Connection success"; 
} 
?> 

loadDatabase.php :

<?php 
require_once'config.php'; 

$query = "select * from lebensmittel"; 
$result = $mysqli->query($query); 

if(mysqli_num_rows($result) > 0) 
{ 
    echo " Tupels vorhanden"; 
    while ($row = $result->fetch_assoc()) 
    { 
     $data[] = $row; 
    } 
} 

header('Content-Type: application/json'); 
echo json_encode($data); 
?> 

controller.js :

angular.module('starter.controllers', []) 
.controller('AppCtrl', function ($scope, $ionicModal, $timeout, $http) { 

    $scope.items = []; 

    function loadData() 
    { 
     $http.get("http://localhost/4n/db_zugriff/loadDatabase.php").success(function (data) { 
      $scope.items = data; 
     }).error(function() { 
      console.log("Error: can't load from php"); 
     }); 
    } 

    loadData(); 

/* 
$scope.items = [ 
    { id: 1, name: 'milk', date: '01-01-2016', amount: 1000 }, 
    { id: 2, name: 'cheese', date: '01-01-2016', amount: 250 }, 

]; 
    */ 

}); 

output.html :

<ion-view view-title="Output"> 
    <ion-content data-ng-app="starter" ng-controller="AppCtrl"> 

     <ion-list > 
      <ion-item ng-repeat="item in items"> 
       {{item.name}} {{item.date}} 
      </ion-item> 
     </ion-list> 

    </ion-content> 
</ion-view> 
,

어떻게 해결할 수 있습니까? 도움을 주셔서 감사합니다.

답변

0
귀하의 NG-응용 프로그램이 잘못

, 그것은,

<ion-content data-ng-app="starter.controllers" ng-controller="AppCtrl"> 

는 또한 함수는 다음과 같이 선언 및 NG-초기화에 전화를해야

$scope.loadData = function() 
{ 
$http.get("http://localhost/4n/db_zugriff/loadDatabase.php").success(function              (data) { 
      $scope.items = data; 
     }).error(function() { 
      console.log("Error: can't load from php"); 
     }); 
    }); 
} 

HTML :

<ion-view view-title="Output"> 
     <ion-content data-ng-app="starter.controllers" ng-controller="AppCtrl"> 
      <ion-list ng-init="loadData()"> 
       <ion-item ng-repeat="item in items"> 
        {{item.name}} {{item.date}} 
       </ion-item> 
      </ion-list> 
     </ion-content> 
    </ion-view> 
+0

도움 주신데 감사드립니다. 또한 json_encode가 작동하지 않도록 데이터베이스가 utf-8로 변환되지 않았 음을 알았습니다. – Acio43

관련 문제