简体   繁体   English

如何使用AngularJs,Ui-Router和MongoDB轮询数据库并填充控制器?

[英]How do I poll a database and populate a controller using AngularJs, Ui-Router, and MongoDB?

I've been trying to get my Angular app to populate the controller with data from a database (mongodb) before the page loads. 我一直在尝试让我的Angular应用程序在页面加载之前用数据库(mongodb)中的数据填充控制器。 I can't quite get it to work. 我不能完全正常工作。 I'm trying to use the "resolve" property of angular ui-router but it's not working and I can't figure it out!! 我正在尝试使用角度ui路由器的“ resolve”属性,但是它不起作用,我无法弄清楚!!

Here's the full code for my app: 这是我的应用程序的完整代码:

var freezerApp = angular.module('freezerApp', ['ui.router']);



    freezerApp.config([
    '$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider) {

    $stateProvider
    .state('home', {
      url: '/home',
      templateUrl: '/partials/home.html',
      controller: 'freezerCtrl',


    });




    $stateProvider
    .state('freezer', {
      url: '/freezers',
      templateUrl: 'partials/freezers.html',
      controller: 'freezerCtrl',
      //not working:


      resolve: {
        freezerPromise: function($scope){
      return $scope.getAll();

      }
    };
    });


  $urlRouterProvider.otherwise('home');
}]);







freezerApp.controller('freezerCtrl', ['$scope', '$http', function ($scope,$http) {

    $scope.freezer = 
    {'freezername':'Freezer Name',
    'building':'Building',
    'floor':'Floor',
    'room':'Room',
    'shelves': 0,
    'racks': 0


    };

    $scope.add_freezer = function() {




    $scope.freezers.push(

    {'freezername': $scope.freezer.freezername,
    'building':$scope.freezer.building,
    'floor':$scope.freezer.floor,
    'room':$scope.freezer.room,
    'shelves': $scope.freezer.shelves,
    'racks': $scope.freezer.racks


    }




        );



};

  $scope.freezers = [
    {
    }


    ];



  $scope.default_freezer = $scope.freezers[0];

  $scope.getAll = function() {
    return $http.get('/freezers').success(function(data){
      angular.copy(data, $scope.freezers);
    });
  }; 




}]);

According to the Ui-Router Resolve Documentation : 根据Ui-Router解决文档

You can use resolve to provide your controller with content or data that is custom to the state. 您可以使用resolve向控制器提供针对该州定制的内容或数据。 resolve is an optional map of dependencies which should be injected into the controller. resolve是一个可选的依赖项映射,应将其注入到控制器中。

If any of these dependencies are promises, they will be resolved and converted to a value before the controller is instantiated and the $stateChangeSuccess event is fired. 如果这些依赖中的任何一个是承诺,则将在实例化控制器和触发$ stateChangeSuccess事件之前将其解析并转换为值。

It looks like you are trying to provide $scope object from your freezerCtrl to your resolve property. 似乎您正在尝试将$scope对象从freezerCtrl提供给resolve属性。 This is incorrect. 这是不正确的。

I would recommend you create a factory like so for your api call. 我建议您为api调用创建一个工厂。

angular.module.('freezerApp').factory('freezerFact',function($http){
    return {
     getAll: $http.get('/freezers')
    }
});

Then inside of your freezer $state deceleration you could do it like this: 然后在您的冰箱$state减速中,您可以像这样进行操作:

$stateProvider
    .state('freezer', {
      url: '/freezers',
      templateUrl: 'partials/freezers.html',
      controller: 'freezerCtrl',
      resolve: {
        freezerPromise: function(freezerFact){
              return freezerFact.getAll;
      }
    };
});

Then you would pass the freezerPromise object into your freezerCtrl and manipulate the promise after that. 然后,您可以将freezerPromise对象传递到您的freezerCtrl然后再处理promise。

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

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