באנגולר אפשר לכתוב [(ngModel)] , זה נותן לנו חיבור דו כיווני (קריאה וכתיבה).
האם ניתן לעשות את זה לכל משתנה שנרצה ?????
אז…..כן, זה ניתן.
משתנה שמקבל מבחוץ נתונים מוגדר
@Input()
משתנה שמוציא החוצה נתונים מוגדר
@Output()
איך משלבים אותם למשתנה אחד?
יש לאנגולר שיטה ספציפית לזהות שהמשתנים הם אותו אחד, המשתנים חייבים להיות עם אותו שם כאשר לשני מתווסף המילה Change בסוף.
ב-PARENT רושמים את המשתנה בתוך [( )] ואנגולר מזהה את זה ומבצע את החיבור.
<p>home-layout works!</p>
<app-tooltip [(doubleBinding)]="this.parentVal"></app-tooltip>
<div>PARENT:{{parentVal}}</div>import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home-layout',
templateUrl: './home-layout.component.html',
styleUrls: ['./home-layout.component.css']
})
export class HomeLayoutComponent implements OnInit {
parentVal: string = 'HI';
constructor() { }
ngOnInit() {
}
}<p>tooltip works!</p>
<div>
CHILD:{{doubleBinding}}
</div>
<button (click)="go()">GO</button>import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
@Component({
selector: 'app-tooltip',
templateUrl: './tooltip.component.html',
styleUrls: ['./tooltip.component.css']
})
export class TooltipComponent implements OnInit {
@Input() doubleBinding: string = '';
@Output() doubleBindingChange: EventEmitter<string> = new EventEmitter<string>();
constructor() { }
ngOnInit() {
}
go() {
this.doubleBindingChange.emit('GO CLICK!!!!');
}
}תוצאה לפני לחיצה על הכפתור:

תוצאה לאחר לחיצה על הכפתור:

אפשר לראות ש-"HI" מהאבא הגיע לילד
ולחיצה על הכפתור בילד שינה אצל האבא את הערך.





