[英]ng-select just return the word 'array"
i have a ng-select in a contact form, I recieve correctly the form except ng-select. 我在联系表单中有一个ng-select,我可以正确接收ng-select以外的表单。
The system just return me the word "array", but I need the selected option from the ng-select. 系统仅向我返回“ array”一词,但我需要从ng-select中选择选项。
This is my portion of html 这是我的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>
This is the code of controller.js 这是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';
}
}
});
And this is the Php 这是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);
}
Can you help me? 你能帮助我吗?
Thanks, Luiggi 谢谢,路易吉
You are having the entire object be set for $scope.myDepartment. 您需要为$ scope.myDepartment设置整个对象。 Instead of 代替
ng-options="departamento.name for departamento in departamento"
try using 尝试使用
ng-options="departamento.name as departamento.name for departamento in departamento"
this should set $scope.myDepartment to the actual string selected, rather than an object of {name: selection} 这应将$ scope.myDepartment设置为所选的实际字符串,而不是{name:selection}对象
Firstly, your ng-options syntax is wrong. 首先,您的ng-options语法错误。 It should be 它应该是
ng-options="singular.title for singular in plural"
If you had an array of objects like this: 如果您有这样的对象数组:
$scope.departamento = [{name: dep1}, {name: dep2}]
You would write ng-options like this: 您将这样编写ng-options:
ng-options="d.name for d in departamento"
Where d is any variable name. 其中d是任何变量名。
Also I don't think you should be doing this 我也不认为你应该这样做
data : $.param($scope.formData)
Because $scope.formData is already a javascript object, and $http, unlike $.ajax automatically stringify's your objects. 因为$ scope.formData已经是一个javascript对象,而$ http与$ .ajax不同,它会自动将您的对象字符串化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.