[英]Error "TypeError: Cannot read property 'email' of undefined" using AngularJS and Ionic
I have the following form using AngularJS and Ionic:我使用 AngularJS 和 Ionic 有以下形式:
<ion-modal-view class="game" ng-controller="PdfsmtpCtrl">
<ion-header-bar class="bar bar-balanced">
<a class="button button-icon icon ion-close-circled" ng-click="hideInformation()"></a>
<h1 class="title">PDF-Export per Email</h1>
<a type="submit" ng-click="getPdf(user)" class="button button-icon icon ion-checkmark-circled"></a>
</ion-header-bar>
<ion-content scroll="false" class="game">
<div>
<form novalidate class="simple-form">
<label>E-mail: <input type="email" ng-model="user.email" /></label><br />
<input type="submit" ng-click="getPdf(user)" value="Save" />
</form>
</div>
</ion-content>
</ion-modal-view>
UPDATE: Here is the related AngularJS code:更新:这是相关的 AngularJS 代码:
'use strict';
angular.module('app')
.controller('PdfsmtpCtrl', function ($scope, Pdfsmtp)
{
$scope.pdfsmtp = new Pdfsmtp();
$scope.hideInformation = function ()
{
$scope.pdfsmtpModal.hide();
};
//joey: PDF to generate
$scope.getPdf = function (user)
{
var user = angular.copy(user);
var emailReceiver = user.email;
var emailSubject = "<SubjectText>";
var emailBody = "<BodyText>";
var emailSender = "<EmailAddress>";
var fileName = "<FileName.pdf>";
var hostName = "<SenderHostName>";
var contentType = "application/pdf";
var doc = new jsPDF
({
orientation: 'p', // p = portrait, l = landscape
unit: 'mm',
format: [210, 297],
});
//joey: Simon Bengtsson and his autotable
doc.setFontSize(22);
doc.setTextColor(23, 154, 85);
doc.text(("<SomeText>"), 14, 15);
doc.autoTable(
{
startY: 30,
startX: 30,
headStyles: {fillColor: [25, 141, 79] },
footStyles: {fillColor: [25, 141, 79] },
theme: 'grid',
styles:
{
overflow: 'linebreak',
lineWidth: 0.5,
lineColor: [25, 141, 79]
},
html: '#resultTable',
});
var finalY = doc.lastAutoTable.finalY || 20;
doc.setTextColor(23, 154, 85);
doc.text('Table: Signature:', 14, finalY + 20);
var blob = doc.output();
var dataUri = "data:" + contentType + ";base64," + btoa(blob);
Email.send(
{
Host: hostName,
Username: emailSender,
Password: "<somepassword>",
To: emailReceiver,
Attachments :
[{
name : fileName,
data : dataUri
}],
From: emailSender,
Subject: emailSubject,
Body: emailBody
}).then($scope.hideInformation());
//}).then(message => alert(message)
}
});
When clicking Save
in the form all goes well as the variable user will be taken over by the related function.当单击表单中的
Save
时一切顺利,因为变量 user 将被相关函数接管。
When clicking the icon for Submit
in <ion-header-bar>
the following error can be seen as the variable user is undefined:单击
<ion-header-bar>
的Submit
图标时,可以看到以下错误,因为变量 user 未定义:
TypeError: Cannot read property 'email' of undefined
类型错误:无法读取未定义的属性“电子邮件”
What needs to be done that Submit
can be used?需要做什么才能使用
Submit
?
You should initialize the user object.您应该初始化用户对象。 That way the object won't be undefined when you are trying to access the email property.
这样,当您尝试访问 email 属性时,对象就不会是未定义的。
$scope.pdfsmtp = new Pdfsmtp();
$scope.user = { email : ''};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.