मैं एक खाका बनाया example.component.html
प्राप्त करता है कि एक विधि के रूप में एक चर करने के लिए अपने क्लिक करें कार्रवाई:
<div>
<button (click)="method()">click here</button>
</div>
पर example.component.ts
फाइल विधि से आता है एक इनपुट () है, तो मैं कर सकते हैं इस टेम्पलेट का उपयोग कई स्थितियों पर:
@Component({
selector: 'example',
templateUrl: './example.component.html',
})
export class ExampleComponent implements OnInit {
@Input() method;
constructor() {}
ngOnInit(): void {}
}
यहाँ है, जहां यह जटिल हो जाता है. पर माता-पिता घटक है, विधि है कि शुरू हो जाएगा पर क्लिक करें का उपयोग करता है एक चर से आ रही एक नमूदार:
parent-example.component.html
<example [method]="onClick"></example>
parent-example.component.ts
@Component({
selector: 'parent-example',
templateUrl: './parent-example.component.html',
})
export class ParentExampleComponent implements OnInit {
@Input() method;
business;
constructor(businessEntityService: BusinessEntityService) {
businessEntityService.entities$.subscribe(
data => (this.business = data),
);
}
onClick() {
console.log(this.business);
}
ngOnInit(): void {}
}
भले ही माता-पिता घटक है सदस्यता businessEntityService
नमूदार और मैं यह जाँच की है, वास्तव में डेटा है, जब मैं बटन क्लिक करें, कंसोल लॉग undefined
.
मैं यह समझ में शायद ऐसा करने के लिए गुंजाइश के साथ और ढेर के लिए लग रही है this.business
पर बच्चों घटक है, हालांकि मैं करने के लिए प्यार होता है पता है कि अगर वहाँ वैसे भी कॉल करने के लिए एक विधि का उपयोग करता है कि एक चर में से एक की सदस्यता से अपने स्वयं के घटक के रूप में इनपुट().