我正在创建一个简单的AngularJS SPA并使用API连接到Mongoose。但是当我只是尝试添加一个新成员时,我一直收到以下错误,该成员确实添加到了猫鼬但在我刷新之前不会在我的网页上看到。当我按添加成员按钮时,我在Google Chrome开发工具中收到以下错误:
TypeError:无法读取属性'成功'未定义的 在Scope。$ scope.addMember(app.js:195)
我不知道如何绕过它。这是我的app.js
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 = { }
} );
}
$scope.membNoInRange = function ()
{
return $scope.memberNo && $scope.memberNo >=0
&& $scope.memberNo < $scope.members.length
}
}])
添加我的工厂服务:
app.factory('SimpleFactory', ['$http', function($http){
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 = $http.get('/api/members/' + member_id )
}
return undefined
}
factory.addMember = function(member) {
$http.post('/api/members',member)
}
factory.updateMember = function(index,member) {
$http.put('/api/members/' + member_id, member)
}
return factory;
}])
任何人都可以提供帮助,我是否需要提供更多信息?
答案 0 :(得分:1)
服务的功能不会返回承诺。
尝试在$ http ...
之前添加returnfactory.addMember = function(member) {
return $http.post('/api/members', member)
}
factory.updateMember = function(index,member) {
return $http.put('/api/members/' + member_id, member)
}