使用API​​和Mongoose的AngularJS SPA

时间:2015-04-25 09:22:03

标签: javascript angularjs node.js api mongoose

我正在创建一个简单的AngularJS SPA,使用API​​将数据加载到Mongoose。

我的应用只是添加,显示和编辑成员列表。当我在工厂服务中将成员存储在一个数组中时,它可以正常工作,但现在我想将其更改为通过API连接到Mongoose。

工厂

app.factory('SimpleFactory', ['$http', function($http){

    var factory = {};

    var members =  $http.get('/api/members') 


    factory.getMembers = function () 
    {
        return members = $http.get('/api/members');
    }

   factory.getMember = function (index) {
        if (index >=0 && index < members.length ) {
           return  members[index] = $http.get('/api/members/' + member_id )
       }
       return undefined
    }


    factory.addMember = function(member) {
         return $http.post('/api/members',member)
    }
    factory.updateMember = function(index,member) {
         $http.put('/api/members/' + member_id, member)
    }

    return factory;
}])

控制器

app.controller('MembersController', ['$scope','SimpleFactory',
   function ($scope,SimpleFactory) {


    SimpleFactory.getMembers()
    .success(function(members) {
    $scope.members = members;
    });


$scope.addMember = function()
{
    var member = {
        name: $scope.newMember.name,
        address: $scope.newMember.address,
        age : $scope.newMember.age,
        level : $scope.newMember.level,
        swimmer : $scope.newMember.swimmer,
        email : $scope.newMember.email,
        regdate : $scope.newMember.regdate,
                }

      SimpleFactory.addMember(member)
      .success(function(added_member) 
      { 
         $scope.members.push(added_member);
         $scope.newMember = { }
      }       );
}

}])

但我不知道如何更改我的控制器以更新成员,它编码如下,以便在我的工厂设置中从数组中选取成员,如何编码以通过API从Mongoose中获取成员:

app.controller('MemberDetailController', ['$scope', '$location', '$routeParams', 'SimpleFactory',
    function($scope, $location, $routeParams, SimpleFactory) {

        $scope.member = {
            index: $routeParams.member_index,
            detail: SimpleFactory.getMember($routeParams.member_index)
        }


        $scope.updateMember = function() {
            SimpleFactory.updateMember($scope.member.index,
                $scope.member.detail)
            $location.path('/members')
        }

    }
])

任何人都可以提供帮助,它不是一个复杂的应用程序,但我只是在学习而且我被困在这里!

由于

1 个答案:

答案 0 :(得分:1)

您应该在getMember保证成功后设置$scope.member对象。

<强>代码

SimpleFactory.getMember($routeParams.member_index).then(function(data){
    $scope.member = {  
      index : $routeParams.member_index, 
      detail : data.user
    };
});

除此之外,您需要确保getMember方法应始终在索引为0时返回承诺

   factory.getMember = function (index) {
        var deferred = $q.defer();
        if (index >=0 && index < members.length ) {
           return members[index] = $http.get('/api/members/' + member_id )
       }
       deferred.resolve;
    }

<强>更新

要调用更新方法,您需要先更改服务,这将返回一个承诺

factory.updateMember = function(index,member) {
     return $http.put('/api/members/' + member_id, member)
}

然后致电factory.updateMember解决该承诺,然后执行$location.path

$scope.updateMember = function() {
    SimpleFactory.updateMember($scope.member.index, $scope.member.detail)
    .then(function(data) {
        $location.path('/members')
    });
};