1

我正在尝试向“twitter”键添加一个值,该键是一个子文档“社交”,在控制器中带有:this.social.twitter。使用它会中断表单提交,控制台中没有错误。如果我将其注释掉并提交表单,它会提交并使用 twitter 和 facebook 将社交添加到带有空字符串的 mongodb。

当表单提交时,它会添加名称和图像的值。

我的架构:

var McSchema = new Schema({
name: {
    type: String,
    default: '',
    required: 'A name is required',
    trim: true
},
image: {
    type: String,
    default: '',
    required: 'An image is required',
    trim: true
},
social: {
    twitter: {
        type: String,
        default: '',
        trim: true
    },
    facebook: {
        type: String,
        default: '',
        trim: true
    }
},
created: {
    type: Date,
    default: Date.now
},
user: {
    type: Schema.ObjectId,
    ref: 'User'
}

});

我的控制器:

angular.module('mcs').controller('McsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Mcs',
function($scope, $stateParams, $location, Authentication, Mcs) {
    $scope.authentication = Authentication;

    // Create new MC
    $scope.create = function() {
        // Create new Mc object
        var mc = new Mcs ({
            name: this.name,
            image: this.image,
            twitter: this.social.twitter
        });

        // Redirect after save
        mc.$save(function(response) {
            $location.path('mcs/' + response._id);

            // Clear form fields
            $scope.name = '';
            $scope.image = '';
            $scope.twitter = '';
        }, function(errorResponse) {
            $scope.error = errorResponse.data.message;
        });
    };

    ...

]);

我的表格:

<section data-ng-controller="McsController">
<div class="page-header">
    <h1>New Mc</h1>
</div>
<div class="col-md-12">
    <form class="form-horizontal" data-ng-submit="create()" novalidate>
        <fieldset>
            <div class="form-group">
                <label class="control-label" for="name">Name</label>
                <div class="controls">
                    <input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="image">Image</label>
                <div class="controls">
                    <input type="text" data-ng-model="image" id="image" class="form-control" placeholder="image" required>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="social-twitter">Twitter</label>
                <div class="controls">
                    <input type="text" data-ng-model="mc.twitter" id="social-twitter" class="form-control" placeholder="@">
                </div>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-default">
            </div>
            <div data-ng-show="error" class="text-danger">
                <strong data-ng-bind="error"></strong>
            </div>
        </fieldset>
    </form>
</div>

4

1 回答 1

0

您已data-ng-model="mc.twitter"在 html 中设置,但在您尝试访问的 javascript 中设置this.social.twitter。如果我没有错过任何东西,那就是你的问题。您应该更改其中任何一个以使它们相同,例如将您的 html 更改为: data-ng-model="social.twitter"

于 2014-09-14T22:59:49.520 回答