@Output與@Input理解
Output和Input是兩個裝飾器,是Angular2專門用來實現跨組件通訊,雙向綁定等操作所用的。
@Input
Component本身是一種支持 nest 的結構,Child和Parent之間,如果Parent需要把數據傳輸給child并在child自己的頁面中顯示,則需要在Child的對應 directive 標示為 input。
形如:
@Input() name: number;
我們通過一個例子來分析一下@Input的流程。
流程:
- child_component.ts內有students,并且是被Input標記的,那么這個屬性作為輸入屬性
- 在parent_component.html內直接使用了students,那是因為在parent.module.ts內將child組件import進來了
- [students]這種形式叫屬性綁定,綁定的值為school.schoolStudents屬性
- Angular會把schoolStudents的值賦值給students,然后影響到子組件的顯示 <