Angular 5变量未定义

时间:2018-08-02 10:20:45

标签: angular angular5

我想在Angular 5中提交一个表单。我的动机是捕捉表单值,并在变量中定义一些静态值。

下面是表单摘要。

<main class="container my-3 my-md-5">
	<form id="paymentForm" method="POST" (ngSubmit)="sendPaymentDataToAnet()" #payFrm="ngForm">
	    <input type="text" maxlength="16" name="cardNumber" id="cardNumber" placeholder="cardNumber" ngModel [(ngModel)]="cardNumber"/> <br><br>
	    <input type="text" maxlength="2" name="expMonth" id="expMonth" placeholder="expMonth"/> <br><br>
	    <input type="text" maxlength="2" name="expYear" id="expYear" placeholder="expYear"/> <br><br>
	    <input type="text" maxlength="3" name="cardCode" id="cardCode" placeholder="cardCode"/> <br><br>
	    <input type="hidden" name="dataValue" id="dataValue" />
	    <input type="hidden" name="dataDescriptor" id="dataDescriptor" />
	    <button type="submit" >Pay</button>
	</form>
</main>

现在在name.component.ts中,我将变量定义为:

export class PAYComponent implements OnInit {
    authData: any;
    // bla bla

这是表单提交代码:

sendPaymentDataToAnet(form: NgForm) { 
authData.clientKey = "XXX2";
}

但是,提交表单时,我在控制台中看到以下错误:

    ReferenceError: authData is not defined
        at PAYComponent.push../src/app/features/pay/pay.component.ts.PAYComponent.sendPaymentDataToAnet
...

在Javascript中,可以将authData声明为:var authData = {}; 根据Angular指南,我将该变量声明为authData: any;

我怎么了?

1 个答案:

答案 0 :(得分:4)

您忘记了this

sendPaymentDataToAnet(form: NgForm) { 
  this.authData.clientKey = "XXX2";
}