2015-11-11 22 views
1

Я новичок в угловых js, я хочу получить данные из базы данных с помощью php. Я пробую следующий код.извлекать данные из databse в угловых js, используя php

  <html> 
      <head> 
       <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> 
<script> 
    var myApp = angular.module('myApp',[]); 
    myApp.controller('studentController', ['$scope','$http', function ($scope,$http) { 
        var url = "ajax.php"; 
        $http.get(url).success(function(response) { 
         $scope.students = response; 
         }); 
       }]); 


       </script> 
      </head> 
      <body> 
       <div ng-app = "myApp" ng-controller = "studentController"> 
       <table> 
        <tr> 
         <th>Name</th> 
        </tr> 

        <tr ng-repeat = "student in students"> 
         <td>{{ student.Name }}</td> 
        </tr> 
       </table> 
       </div> 
      </body> 
     </html> 

ajax.php этот код дает мне следующий вывод:

[{"Name":"Mahesh","Roll":"122222"},{"Name":"ajay","Roll":"444433"}] 

, но я не в состоянии отобразить на переднем конце ... Спасибо.

<?php 
    $con = mysqli_connect("localhost","root","","adworld"); 
    $res= mysqli_query($con,"SELECT * FROM students"); 
    $result = array(); 
    while($row=mysqli_fetch_assoc($res)){ 
     $result[] = $row; 
    } 
    echo json_encode($result); 
?> 
+0

вам необходимо зарегистрировать контроллер в вашем приложении - 'myApp.controller ("studentController", [ '$ Сфера' , function ($ scope) { $ scope.greeting = 'Hola!'; }]); ' – messerbill

+0

https://docs.angularjs.org/guide/controller – messerbill

+0

@messerbill: не работает .... –

ответ

0

Рабочий код:

<!DOCTYPE html> 
<html > 
<script src= "angular.js"></script> 
<body> 

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table> 
    <tr> 
    <th>Name</th> 
    <th>Roll</th> 
    </tr> 
    <tr ng-repeat="x in names"> 
    <td>{{ x.Name }}</td> 
    <td>{{ x.Roll}}</td> 
    </tr> 
</table> 

</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('customersCtrl', function($scope, $http) { 
    $http.get("ajax.php") 
    .success(function (response) {$scope.names = response;}); 
}); 
</script> 

</body> 
</html> 

ajax.php

<?php 
    $conn = new mysqli("localhost","root","","adworld"); 
    $result = $conn->query("SELECT * FROM students"); 
    $arr = array(); 
    while($rs = $result->fetch_assoc()) 
    { 
     $arr[] = $rs; 
    } 

    echo json_encode($arr); 
    ?> 
Смежные вопросы