简体   繁体   English

使用 AngularJS 和 Ionic 出现错误“TypeError:无法读取未定义的属性‘email’”

[英]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.

相关问题 Angularjs / Ionic TypeError:无法读取未定义的属性&#39;then&#39; - Angularjs/Ionic TypeError: Cannot read property 'then' of undefined 错误TypeError:无法读取未定义的属性&#39;名称&#39;-使用angularjs - ERROR TypeError: Cannot read property 'name' of undefined - Using angularjs TypeError:无法使用AngularJs读取IONIC中未定义的属性&#39;googleplus&#39; - TypeError: Cannot read property 'googleplus' of undefined in IONIC with AngularJs 错误类型错误:无法读取未定义的属性“toLowerCase”。 离子 3 - ERROR TypeError: Cannot read property 'toLowerCase' of undefined. Ionic 3 错误类型错误:无法读取未定义 Ionic/Firestore 的属性“用户名” - ERROR TypeError: Cannot read property 'username' of undefined Ionic/Firestore AngularJS错误:TypeError:无法读取未定义的属性“图像” - AngularJS error : TypeError: Cannot read property 'images' of undefined AngularJS + Fullcalendar发送错误TypeError:无法读取未定义的属性&#39;__id&#39; - AngularJS + Fullcalendar send error TypeError: Cannot read property '__id' of undefined AngularJS 1.4错误-未捕获的TypeError:无法读取未定义的属性“ apply” - AngularJS 1.4 error - Uncaught TypeError: Cannot read property 'apply' of undefined Ionic:TypeError:无法读取未定义的属性“ ready” - Ionic: TypeError: Cannot read property 'ready' of undefined TypeError:无法使用AngularJS从数组读取未定义的属性“ 0” - TypeError: Cannot read property '0' of undefined from the Array using AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM