doumianfeng6979 2015-12-10 10:14
浏览 62

AngularJS $ http-Service的问题

The following code should fetch data from a MySQL Database with a $http-service but somehow the data, which the service provides doesn´t go to the controller/gets displayed wrong :/. If I start my application a empty or wrong table gets displayed at my page.

I provided some sampledata in the getUsers() function. The PHP code, which provides the data is working and this is a example of the JSON the PHP returns.

Maybe you guys can help me out to fix this up.

var app = angular.module('DBapp', []);

app.controller('MainCtrl', function($scope, $http, DatabaseService) {
    
  $scope.userData = DatabaseService.getUsers();
    
});

app.factory('DatabaseService', function($http) {
    
    var srv = {};
    
    //Service Implementation
    
    srv.getUsers = function() {
      return [{"user_id":"1","firstname":"Barrack","lastname":"Obama","email":"obama@web.de"},      {"user_id":"2","firstname":"Tom","lastname":"Hanks","email":"tommy@web.de"},{"user_id":"6","firstname":"Felix","lastname":"Blume","email":"felix.b@selfmade.de"}];
    };
  
  
    //I provided some dummy-data in the getUsers-function as example output from the DB
  
    
    //Public API
    
    return {
        getUsers: function() {
            return srv.getUsers();
        }
        
    };
    
});
<!DOCTYPE html>
<html ng-app="DBapp">
<head>
<meta charset="ISO-8859-1">
<title>DB Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

</head>

<body ng-controller="MainCtrl">
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <div>
            
      <div>
        <div class="page-header">
          <h1>Overview Database</h1>
      </div>
                        
      <table style="font-size:20px; margin-left:10px;" class="table table-hover"">
        
      <thead><tr><th>ID</th><th>Firstname</th><th>Secondname</th><th>Email</th></tr></thead>
        
      <tbody>
        <tr ng-repeat="user in userData">
             <td ng-bind="user.user_id"></td>
             <td ng-bind="user.firstname"></td>
             <td ng-bind="user.lastname"></td>
             <td ng-bind="user.email"></td>
             <td>
               <a ng-href="#/database/{{ user.user_id }}/edit"><button type="button" class="btn btn-info"><span class="glyphicon glyphicon-wrench"></span>  Edit</button></a>
                                                                                                                      </td>
                      <td>                                                                                                      <a ng-href="#/database/{{ user.user_id }}/delete"><button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span>  Delete</button></a></td>
                
            </tr>
        </tbody>
    </table>
    
    <add-user-form></add-user-form>
</div>
                
            </div>
        </div>
    </div>

<!-- SCRIPTS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src=lib/jquery/jquery-1.10.2.min.js></script>
<script src=lib/angular/angular.js></script>
<script src=lib/angular-route/angular-route.js></script>

<script src=scripts/app.js></script>
<script src=scripts/services/db_service.js></script>
<script src=scripts/controllers/main_controller.js></script>
<script src=scripts/directive/addUser_form.js></script>
<script src=scripts/controllers/delete_controller.js></script>
<script src=scripts/controllers/edit_controller.js></script>

</body>
</html>


As you can see above, it works with the dummy data. But if I change my service to the following code (with the actual $http-request) nothing works...

So the question is:

How do I pass my data from the http request to the $scope in the MainCtrl ?

app.factory('DatabaseService', function($http) {
    
    var srv = {};
    
    //Service Implementation
    
    srv.getUsers = function() {
        $http.post("php/getData.php").success(function(data) {
            return data;
        });
    };
    
    //Public API
    
    return {
        getUsers: function() {
            return srv.getUsers();
        }
        
    };
    
});

`

</div>
  • 写回答

1条回答 默认 最新

  • dongpa9277 2015-12-10 10:25
    关注

    It probably will work if you just return the array from your service like

    return {
        getUsers: function() {
            return srv.getUsers;
        }
    };
    

    The function call you used is not possible.

    Ok here comes part 2: I had no chance to test it, but it should work.

    app.factory('DatabaseService', function($http) {    
        var srv = {};
    
        //Service Implementation        
        $http.post("php/getData.php").success(function(data) {
                srv.users = data;
            });     
    
        //Public API        
        return {
            getUsers: function() {
                return srv.users;
            }
        };
    });
    

    Jorg is right, this part 2 was a hasty reaction, sorry. To return the promise is a possible solution, return $http.post("php/getDat.php"), and than in your controller add the success function

    DatabaseService.getUsers().success(function(data) {
      $scope.userData = data;
      });

    Hope that helps

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?