如何根据选择器更改切换组件?

时间:2016-07-27 16:25:23

标签: angular

我有一个选择器,可让用户更改所选的付款方式。这称为一个名为PaymentComponent的组件,它“应该”监听父组件中Selector的更改,然后切换相应的支付方法组件(即WireTransferPayment,CheckPayment,CreditCardPayment),但现在不起作用。事实上,PaymentComponent甚至没有注册选择器已经改变。我还在学习Angular2所以我意识到我仍然缺少一些简单的东西。有人能指出我正确的方向吗?我的google foo现在让我失望了。

print(format(MyArray_precise[0], '.64'))
# 0.442000000000000003996802888650563545525074005126953125

1 个答案:

答案 0 :(得分:0)

所以,我找到了解决方案,import {Component, OnInit, Input, OnChanges, SimpleChange} from '@angular/core'; @Component({ moduleId: module.id, selector: 'app-payment', templateUrl: 'payment.component.html', styleUrls: ['payment.component.css'], directives: [ CheckPaymentComponent, WireTransferPaymentComponent ], }) export class PaymentComponent implements OnInit, OnChanges { @Input() name: string; changeLog: string[] = []; ngOnChanges(changes: {[propkey: string]: SimpleChange}) { let log: string[] = []; console.log('log ', log); for (let propName in changes) { console.log('propName ', propName); let changedProp = changes[propName]; let from = JSON.stringify(changedProp.previousValue); let to = JSON.stringify(changedProp.currentValue); log.push( `${propName} changed from ${from} to ${to}`); } this.changeLog.push(log.join(', ')); } constructor() { } ngOnInit() { } } 就是我所需要的。

{site:randomNumber(min: 1, max:10)}