我正在尝试在模板上打印razorpay给出的银行列表 我得到所有银行列表对象作为响应,并将其保存在变量库中,但是此变量在模板中没有任何作用,即我看不到任何值
我尝试打印以下代码,但我从未看到Hello可以打印
<h2 *ngIf="banks">Hello</h2>
下面是我来自component.ts的摘录
ngOnInit() {
if (this.common.platformCheck()) {
var razorpay = document.createElement('script');
razorpay.id = 'razorPay'
razorpay.src = 'https://checkout.razorpay.com/v1/razorpay.js';
razorpay.async = true;
razorpay.type = "text/javascript";
var scpt = document.getElementsByTagName('script')[0];
scpt.parentNode.insertBefore(razorpay, scpt);
razorpay.onload = () => {
var options = {
"key": "rzp_test_XXXXXXXXXXX",
"image": 'https://i.imgur.com/n5tjHFD.png'
};
var razorpay = this.windowObj.Razorpay(options);
razorpay.once('ready', function (response) {
this.banks= response.methods.netbanking;
console.log(response.methods);
console.log(razorpay.methods.netbanking);
// response.methods.netbanking contains list of all banks
})
};
}
}
答案 0 :(得分:2)
razorpay.once('ready', function (response) {
this.banks= response.methods.netbanking;
console.log(response.methods);
console.log(razorpay.methods.netbanking);
// response.methods.netbanking contains list of all banks
})
问题出在function
关键字中,因为在该功能块中使用的this
与功能本身相关,而不是组件的this
。只需将其更改为此:
razorpay.once('ready', (response) => {
this.banks= response.methods.netbanking;
console.log(response.methods);
console.log(razorpay.methods.netbanking);
// response.methods.netbanking contains list of all banks
})
它应该工作,因为this
将引用组件的this
,并填充正确的组件属性。
有关...“ this”的更多阅读内容: