Angular父子Component之间的事件通知机制
生活随笔
收集整理的這篇文章主要介紹了
Angular父子Component之间的事件通知机制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
例子:product-list為parent Component,product-alert為child Component.
(1) child Component里必需的開發
從@angular/core里導入Input, Output和EventEmitter:
新建一個名為product的屬性,用于從parent Component里接收當前選中的product.
新建一個EventEmitter實例,用于向parent Component發送事件。
在product alert模板里,給按鈕點擊click事件附上處理函數:notify.emit()
<p *ngIf="product.price > 700"><button (click)="notify.emit()">Notify Me</button> </p>(2) parent Component里必需的開發
使用語法中括號[]給child Component的Input屬性賦值,使用語法大括號{}給child Component的Output屬性賦值:
onNotify的實現,位于parent Component ts文件內:
最后的效果:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
總結
以上是生活随笔為你收集整理的Angular父子Component之间的事件通知机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 银行卡里的子账户是什么,附属账户
- 下一篇: 如何使用Angular的@Input()