将数据从节点服务返回到angularjs控制器

时间:2016-07-11 10:32:42

标签: angularjs node.js mongodb

我正在关注Mean Machine书。其中一个示例是连接到mongo db并检索所有用户。这本书似乎有点过时了(已经修复了2-3个错误)。

问题是我的服务没有连接到mongodb并返回数据。从UI,服务返回当前页面的html。表格未填充。

但是当我通过邮递员调用服务时,我得到了数据

用户检索的代码就是这个。

  

用户服务:

angular.module('userService', [])

.factory('User', function($http) {

    //create new object for factory
    var userFactory = {};

    //get single user
    userFactory.get = function(id) {

        return $http.get('/api/user/' + id);
    };

    //get all users
    userFactory.all = function() {
        return $http.get("/api/user/");
    };
...

仅显示上述相关部分。

  

控制器:

angular.module('userCtrl', ['userService'])

.controller('userController', function(User) {
    var vm = this;
    //processing control
    vm.processing = true;

    //Grab all users on load
    User.all()
        .then(function(udata) {
            console.log(udata.data);
            vm.processing = false;
            vm.users = udata.data;
        });
})
  

HTML(all.html)

<tr ng-repeat="person in user.users">
            <td>{{person._id}}</td>
            <td>{{person.name}}</td>
            <td>{{person.username}}</td>
            <td class="col-sm-2">
                <a ng-href="/users/{{ person._id }}" class="btn btn-danger">Edit</a>
            </td>
        </tr>
  

主角文件

angular.module('userApp', [
    'ngAnimate',
    'app.routes',
    'authService',
    'mainCtrl',
    'userCtrl',
    'userService'
])

//app config to integrate token to request
.config(function($httpProvider) {

    //attach auth interceptor to http requests
    $httpProvider.interceptors.push('AuthInterceptor');
})
  

HTML文件(index.html)

<body ng-app="userApp" ng-controller="mainController as main">
....
<main class="container">
            <!-- ANGULAR VIEWS -->
            <div ng-view></div>
        </main>
</body>

用户表已插入上述模板中。

  

路线:

angular.module('app.routes', ['ngRoute'])

.config(function($routeProvider, $locationProvider) {

    $routeProvider
 //users
    .when('/users', {
        templateUrl: 'app/views/pages/users/all.html',
        controller: 'userController',
        controllerAs: 'user'
    });

});

问题是由于版本不匹配造成的吗?请阅读$ http服务已升级。 谢谢你的帮助

0 个答案:

没有答案
相关问题