Angular嵌套重复,保存到数据库不起作用

时间:2015-06-04 08:23:51

标签: angularjs node.js mongoose mean-stack

我正在使用MEAN应用程序,现在我想从我的数据库中读取数据,允许用户编辑它,并更新对数据库的更改。一切都运行正常,除了嵌套重复,我能够得到的值,但不能保存到数据库。

代码:

<div class="controls" ng-repeat="(key, module) in project.website.data.modules">
                    <input type="checkbox" name="modules" value="{{key}}" data-ng-model="module.active" id="{{key}}"><label for="{{key}}"> module: {{key}} = {{module.active}}</label><br>

                    <div class="features_settings" ng-repeat="(key2, feature) in module.data">
                        <input type="text" name="data" value="{{key2}}" data-ng-model="feature" id="{{key2}}"><label for="{{key2}}"> feature {{key2}}, value= {{feature}}</label><br>
                    </div>
                </div>

为uniekewebsitefeature.data.title提供结果: (这是正确的,因为我将默认值设置为“asdf”)

  

功能标题,值= asdf

Mongoose架构:

website: {
    active: { type: Boolean },
    data: { 
        domain: { type: String },
        modules: {
            aboutus: {
                active:{ 
                    type: Boolean,
                    default: false
                },
                data: {
                    title: { 
                        type: String,
                        default: ""
                    },titlerererere: { 
                        type: String,
                        default: ""
                    },
                    text: { 
                        type: String,
                        default: ""
                    }
                }
            },
            contact: {
                active: { 
                    type: Boolean,
                    default: false
                },
                data: {
                    fields: [
                        {
                            type: String,
                            values: [String],
                            label: String,
                            placeholder: String
                        }
                    ],
                    recipient: { 
                        type: String,
                        default: ""
                    }
                }
            },
            uniekewesitefeature: {
                active: { 
                    type: Boolean,
                    default: false
                },
                data: {
                    title: { 
                        type: String,
                        default: "asdf"
                    },
                    text: { 
                        type: String,
                        default: ""
                    }
                }
            }
        }
    }
}

客户端控制器

// Update existing Project
    $scope.update = function() {            
        var project = $scope.project;

        project.$update(function() {
            $location.path('projects/' + project._id);
        }, function(errorResponse) {
            $scope.error = errorResponse.data.message;
        });
    };

服务器控制器

exports.update = function(req, res) {
var project = req.project;

project = _.extend(project , req.body);

project.save(function(err) {
    if (err) {
        return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
        });
    } else {
        res.jsonp(project);
    }
});

};

当我将MODULE设置为active / false时,它工作正常并保存对数据库的更改,但是当我保存该模块的一个FEATURE(嵌套)时,它不会保存到数据库,甚至很难能够从数据库中获取值。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最后修复它,需要进行以下更改:

<div class="controls" ng-repeat="(key, module) in project.website.data.modules">
                    <label for="{{key}}"> module: {{key}} = {{module.active}}</label>
                    <input type="checkbox" name="modules" value="{{key}}" data-ng-model="module.active" id="{{key}}"><br>

                    <div class="features_settings" ng-repeat="(key2, feature) in module.data track by $index">
                        <label for="{{key2}}">Feature: {{key2}} , value = {{module.data[key2]}}</label>
                        <input type="text" class="form-control" name="features" data-ng-model="module.data[key2]" id="{{key2}}"><br>
                    </div>
                </div>

希望它能帮助其他人:)