1

我正在从数据库中调用一些值并将它们放在表单中的选择框中,但是,每当我单击提交时,它都会提交一个空值,我认为这是因为我在表单中使用了多个控制器,从我已经聚集,我必须在控制器中做一些有作用域的事情,但我一直无法做到这一点

附件是创建视图文件的副本,突出显示的部分是多个控制器。请问我如何使它工作?非常感谢

<section data-ng-controller="CandidatesController">
<div class="page-header">
    <h1>New Candidate</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="vision">Vision</label>
                <div class="controls">
                    <textarea data-ng-model="vision" id="vision" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="dob">Date Of Birth</label>
                <div class="controls">
                    <input type="date" data-ng-model="dob" id="dob" class="form-control" required>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="post">Post</label>
                <div class="controls">
                    <select data-ng-model="post" id="post" class="form-control">
                        <option value="Presidential">PRESIDENTIAL</option>
                        <option value="Provincial">PROVINCIAL</option>
                        <option value="Municipal">MUNICIPAL</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="province">Province</label>
                <div class="controls">
                    <select data-ng-model="province" id="province" class="form-control">
                        <option value="Gauteng">Gauteng</option>
                        <option value="Free-State">Free-State</option>
                        <option value="Kwazulu-Natal">Kwazulu-Natal</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="municipal">Municipal</label>
                <div class="controls">
                    <input type="text" data-ng-model="municipal" id="municipal" class="form-control" placeholder="municipal" required>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="party">Party</label>
                <div class="controls">
                    <section data-ng-controller="PartiesController" data-ng-init="find()">
                        <select data-ng-model="party" class="form-control" ng-options="party.name for party in parties track by party.name">

                        </select>
                    </section>
                </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>
</section>

候选控制器代码

 'use strict';
 //Candidates controller
angular.module('candidates').controller('CandidatesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Candidates',
function($scope, $stateParams, $location, Authentication, Candidates ) {
    $scope.authentication = Authentication;

    // Create new Candidate
    $scope.create = function() {
        // Create new Candidate object
        var candidate = new Candidates ({
            name: this.name,
            vision: this.vision,
            dob: this.dob,
            post: this.post,
            province: this.province,
            municipal: this.municipal,
            party: this.party
        });

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

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

    // Remove existing Candidate
    $scope.remove = function( candidate ) {
        if ( candidate ) { candidate.$remove();

            for (var i in $scope.candidates ) {
                if ($scope.candidates [i] === candidate ) {
                    $scope.candidates.splice(i, 1);
                }
            }
        } else {
            $scope.candidate.$remove(function() {
                $location.path('candidates');
            });
        }
    };

    // Update existing Candidate
    $scope.update = function() {
        var candidate = $scope.candidate ;

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

    // Find a list of Candidates
    $scope.find = function() {
        $scope.candidates = Candidates.query();
    };

    // Find existing Candidate
    $scope.findOne = function() {
        $scope.candidate = Candidates.get({ 
            candidateId: $stateParams.candidateId
        });
    };
}

]);

4

0 回答 0