[mysql]在页面加载 AngularJS 和 NodeJS 之前加载数据

发布时间: 2016/10/19 23:21:37
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我是一个小 newie 在 AngularJS 和 NodeJS,我有一个应用程序,我想要从一个 mysql 数据库加载一些数据和加载数据通过节点服务器和角框架,我的问题是,如何可以加载要在之前其加载的网页中显示它的数据吗?我深处它但我总是从 json 变种未定义

这是我的代码︰

var app = angular.module('pedidos',[]);
app.service('adminPedidos',function($http){
    this.getAll = function(){
        var data =[];
        var req = {
            method: 'GET',
            url: 'http://localhost:3000/all',
        };
        return $http(req).then(function(response, status, headers, config) {
            return response;
        }).error(function(response, status, headers, config) {
            alert(response+" "+status);
        });
    };
});

与控制器有以下句子︰

app.controller('Main'['$scope','$http','adminPedidos',function($scope,$http,$adminPedidos){
    this.json = [];
    this.json = $adminPedidos.json;
    console.log(this.json);
}]);

在 html 中我也这︰

<section class="container" ng-controller="Main as controller">
    <section ng-repeat="items in controller.json" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div data-id="{{items.id}}">
            <span class="col-lg-3 col-md-3 col-sm-6 col-xs-12" name="name">{{items.name}}</span>
            <span class="col-lg-3 col-md-3 col-sm-6 col-xs-12" name="link"><a href="{{item.link}}">{{items.link}}</a></span>
            <span class="col-lg-2 col-md-2 col-sm-6 col-xs-12" name="price">{{items.price}}</span>
            <span class="col-lg-1 col-md-1 col-sm-6 col-xs-12" name="quantity">{{items.quantity}}</span>
        </div>
        <div class="col-lg-offset-1 col-md-offset-1 col-lg-1 col-md-2 col-sm-6 col-xs-6"><button id="edit" class="btn btn-warning">Editar</button></div>
        <div class="col-lg-1 col-md-2 col-sm-6 col-xs-6"><button id="delete" class="btn btn-danger">Borrar</button></div>
    </section>
</section>

解决方法 1:

服务

angular.module('pedidos').factory('adminPedidos', ['$http', function ($http) {
  return {
      getAll: function () {
        return $http.get('all'); // or change all to http://localhost:3000/all
      }
   };
}]); 

控制器

angular.module('pedidos').controller('Main'['$scope', 'adminPedidos', function ($scope, adminPedidos) {
        adminPedidos.getAll().success(function (data) {
           $scope.items = data;
        });
}]);

html

<section class="container" ng-controller="Main">
<section ng-repeat="item in items" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
    <div data-id="{{item.id}}">
        <span class="col-lg-3 col-md-3 col-sm-6 col-xs-12" name="name">{{item.name}}</span>
        <span class="col-lg-3 col-md-3 col-sm-6 col-xs-12" name="link"><a href="{{item.link}}">{{item.link}}</a></span>
        <span class="col-lg-2 col-md-2 col-sm-6 col-xs-12" name="price">{{item.price}}</span>
        <span class="col-lg-1 col-md-1 col-sm-6 col-xs-12" name="quantity">{{item.quantity}}</span>
    </div>
    <div class="col-lg-offset-1 col-md-offset-1 col-lg-1 col-md-2 col-sm-6 col-xs-6"><button id="edit" class="btn btn-warning">Editar</button></div>
    <div class="col-lg-1 col-md-2 col-sm-6 col-xs-6"><button id="delete" class="btn btn-danger">Borrar</button></div>
</section>

赞助商