简体   繁体   English

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

[英]Load data before page is loaded AngularJS and NodeJS

i'm a little newie in AngularJS and NodeJS, i have an app that i want to load some data from a mysql db and load that data through Node server and Angular framework, my question is, how can i load the data to show it in a page before its loaded? 我是AngularJS和NodeJS的新手,我有一个应用程序,我想从mysql db加载一些数据并通过Node服务器和Angular框架加载这些数据,我的问题是,如何加载数据以显示它在页面加载之前? i'm tryng it but always i get undefined from json var. 我正在尝试,但总是从json var中得到未定义。

This is my code: 这是我的代码:

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);
        });
    };
});

and the controller have the following sentence: 和控制器具有以下语句:

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

Also in html i have this: 另外在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>

Sadly this is not something that you can completely achieve in a straight forward manner with angular. 遗憾的是,这并不是您可以完全直接地以角度实现的。 If you are loading a lot of data then you will have to hide you view and show only when the data has loaded. 如果要加载大量数据,则必须隐藏视图并仅在数据加载后才显示。 However if you are loading only a little data, the you could give the following code a try: 但是,如果仅加载少量数据,则可以尝试以下代码:

$scope.$on("$ionicView.beforeEnter", function(){
  //Load data here
  //And bind to scope
})

But generally for a solid fix, I always would recommend that you hide the view using ng-show or ng-hide and only show the view once all the data has loaded. 但是通常为了获得可靠的修复,我总是建议您使用ng-show或ng-hide隐藏视图,并且仅在加载所有数据后才显示视图。

A good way would be to use Location Provider ($locationProvider) or State provider ($stateProvider), where you have the ability to load a view after resolving a promise. 一个好方法是使用位置提供程序($ locationProvider)或状态提供程序($ stateProvider),在这里您可以在解决承诺后加载视图。

Or else if you are not using any of these, as in your case, since the service is returning promise, have the view updated when it resolves using 'then' 否则,如果您未使用任何一种方法(例如,由于服务正在返回promise),请在使用“ then”解析时更新视图

    app.controller(){
    this.json = [];
    var self = this;
    adminPedidos.then(function(data){
        self.json = data; //a digest cycle would be required (Depends)
//also, probably use ng-show to show the view (as other person have suggested)
    }, function(err){

    });
    console.log(this.json);
}]);

Service 服务

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

Controller 控制者

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

html 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM