ng-select只返回单词' array"

时间:2014-10-23 21:17:19

标签: javascript php angularjs

我在联系表格中有一个ng-select,我正确地接受了除ng-select之外的表格。

系统只返回“array”一词,但我需要ng-select中的选定选项。

这是我的html部分

<div ng-controller="ContactController" class="panel-body">
        <form ng-submit="submit(contactform)" name="contactform" method="post" action="" class="form-horizontal" role="form">
            <div class="row half" ng-class="{ 'has-error': contactform.inputName.$invalid && submitted }">
                <div class="12u">
                    <input ng-model="formData.inputName" type="text" class="form-control" id="inputName" name="inputName" placeholder="Nombre" required>
                </div>
            </div>
            <div class="row half" ng-class="{ 'has-error': contactform.inputEmail.$invalid && submitted }">
                <div class="12u">
                    <input ng-model="formData.inputEmail" type="email" class="form-control" id="inputEmail" name="inputEmail" placeholder="Email de contacto" required>
                </div>
            </div>
            <!-- select -->
            <div class="row half" ng-class="{ 'has-error': contactform.myDepartment.$invalid && submitted }">
                <div class="12u">
                    <select ng-model="formData.myDepartment" ng-options="departamento.name for departamento in departamento" id="myDepartment" name="myDepartment">
                            <option value="">-- Tipo de mensaje --</option>
                    </select>
                </div>
            </div>
            <!-- select -->
            <div class="row half" ng-class="{ 'has-error': contactform.inputSubject.$invalid && submitted }">
                <div class="12u">
                    <input ng-model="formData.inputSubject" type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Asunto del mensaje" required>
                </div>
            </div>
            <div class="row half" ng-class="{ 'has-error': contactform.inputMessage.$invalid && submitted }">
                <div class="12u">
                    <textarea ng-model="formData.inputMessage" class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Mensaje" required></textarea>
                </div>
            </div>
            <div class="row">
                <div class="12u">
                    <input type="submit" name="submit" id="submit_btn" value="Enviar mensaje" ng-disabled="submitButtonDisabled"/>
                </div>
            </div>
        </form>
        <p ng-class="result" style="padding: 15px; margin: 0;">{{ resultMessage }}</p>
</div>

这是controller.js的代码

app.controller('ContactController', function ($scope, $http) {
    $scope.result = 'hidden'
    $scope.resultMessage;
    $scope.formData; //formData is an object holding the name, email, subject, and message
    $scope.submitButtonDisabled = false;
    $scope.submitted = false; //used so that form errors are shown only after the form has been submitted
    $scope.departamento = [
        {name:'Selecciona departamento'},
        {name:'RRHH'},
        {name:'Soporte'},
        {name:'Postventa'},
        {name:'Comercial'},
        {name:'Desarrollo'}
      ];

    $scope.submit = function(contactform) {
        $scope.submitted = true;
        $scope.submitButtonDisabled = true;
        if (contactform.$valid) {
            $http({
                method  : 'POST',
                url     : '/contact-form.php',
                data    : $.param($scope.formData),  //param method from jQuery
                headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  //set the headers so angular passing info as form data (not request payload)
            }).success(function(data){
                console.log(data);
                if (data.success) { //success comes from the return json object
                    $scope.submitButtonDisabled = true;
                    $scope.resultMessage = data.message;
                    $scope.result='bg-success';
                    $scope.myDepartment = $scope.departamento.name; // red

                } else {
                    $scope.submitButtonDisabled = false;
                    $scope.resultMessage = data.message;
                    $scope.result='bg-danger';
                }
            });
        } else {
            $scope.submitButtonDisabled = false;
            $scope.resultMessage = 'Failed :( Please fill out all the fields.';
            $scope.result='bg-danger';
        }
    }
});

这就是Php

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once 'phpmailer/PHPMailerAutoload.php';

if (isset($_POST['inputName']) && isset($_POST['inputEmail']) && isset($_POST['myDepartment']) && isset($_POST['inputSubject']) && isset($_POST['inputMessage'])) {

    //check if any of the inputs are empty
    if (empty($_POST['inputName']) || empty($_POST['inputEmail']) || empty($_POST['myDepartment']) || empty($_POST['inputSubject']) || empty($_POST['inputMessage'])) {
        $data = array('success' => false, 'message' => 'Completa el formulario antes de enviar');
        echo json_encode($data);
        exit;
    }

    //create an instance of PHPMailer
    $mail = new PHPMailer();

    $mail->From = $_POST['inputEmail'];
    $mail->FromName = $_POST['inputName'];
    $mail->AddAddress('xxxx@xxx.com'); //recipient 
    $mail->Subject = $_POST['inputSubject'];
    $mail->Body = "Name: " . $_POST['inputName'] . "\r\n\r\nDepartment: " . $_POST['myDepartment'] . "\r\n\r\nMessage: " . stripslashes($_POST['inputMessage']);

    if (isset($_POST['ref'])) {
        $mail->Body .= "\r\n\r\nRef: " . $_POST['ref'];
    }

    if(!$mail->send()) {
        $data = array('success' => false, 'message' => 'El mensaje no puede ser enviado. Error: ' . $mail->ErrorInfo);
        echo json_encode($data);
        exit;
    }

    $data = array('success' => true, 'message' => 'Gracias, hemos recibido tu mensaje.');
    echo json_encode($data);

} else {

    $data = array('success' => false, 'message' => 'Rellena el formulario completamente.');
    echo json_encode($data);

}
你能帮帮我吗?

谢谢,Luiggi

2 个答案:

答案 0 :(得分:1)

您正在为$ scope.myDepartment设置整个对象。而不是

ng-options="departamento.name for departamento in departamento" 

尝试使用

ng-options="departamento.name as departamento.name for departamento in departamento" 

这应该将$ scope.myDepartment设置为所选的实际字符串,而不是{name:selection}的对象

答案 1 :(得分:0)

首先,您的ng-options语法错误。它应该是

ng-options="singular.title for singular in plural"

如果你有一个像这样的对象数组:

$scope.departamento = [{name: dep1}, {name: dep2}]

你会写这样的ng-options:

ng-options="d.name for d in departamento"

其中d是任何变量名。

我也不认为你应该这样做

 data : $.param($scope.formData)

因为$ scope.formData已经是一个javascript对象,而$ http与$ .ajax不同,它会自动对你的对象进行字符串化。