【发布时间】:2017-12-22 04:28:02
【问题描述】:
我目前正在尝试在我的 angular4 nodejs 应用程序中实现条带化,但是当我试图通过处理与条带相关的请求的服务将卡令牌发送到我的服务器时,我有点卡住了。这是我得到的代码:
stripe.component.html:
<form role="form" id="payment-form">
<div id="card-element"></div>
<div id="card-errors" role="alert"></div>
<button type="submit" (click)="addCard()">Submit Payment</button>
</form>
stripe.component.ts:
import {Component, OnInit} from "@angular/core";
import {WindowRef} from "../social/windowRef";
import {StripeService} from "./stripe.service";
@Component({
selector: "app-stripe",
templateUrl: './stripe.component.html',
styleUrls: ['./stripe.component.css']
})
export class StripeComponent implements OnInit {
elements: any;
stripe: any;
style: any;
card: any;
constructor(private stripeService: StripeService){}
ngOnInit() {
this.initCardElement();
}
initCardElement(){
this.stripe = WindowRef.get().Stripe("my-key");
this.elements = this.stripe.elements();
this.style =
{
base: {
color: '#32325d',
lineHeight: '24px',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
this.card = this.elements.create('card', {style: this.style});
this.card.mount('#card-element');
}
addCard() {
this.stripe.createToken(this.card)
.then(function (result) {
if (result.error) {
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
console.log(result.token);
this.stripeService.addCard(result.token)
.subscribe((response: Response) => {
console.log(response);
}
);
}
});
}
}
我设法获得了条纹的卡令牌,但是当我尝试调用我的 stripeService 时,我收到了这个错误:
Error: Uncaught (in promise): TypeError: Cannot read property 'stripeService' of undefined
我知道这里没有定义this.stripeService,但我不明白如何解决这个问题。
祝你有美好的一天:)
【问题讨论】:
-
使用箭头功能,你失去了上下文。 function (result){ /* some code / } to (result) => { / some code */ }
标签: node.js angular typescript promise stripe-payments