I´ve using this method to share data between components because is more agile than observables, services or whatever method... But, I´m using on an angular 15 project and it seems doesnt work.
I have a parent component with an object variable initialized as empty object.
Parent component
exhibitor: object = {};
I have a child component with an input variable which i share reference to exhibitor parent variable
@Input() exhibitorData: object = {};
Then, i share exhihibitor parent with child in template
<app-exhibitor-child [exhibitor-data]="exhibitor"></app-exhibitor-child>
But when I change exhibitorData it doesnt affect to exhibitor parent varible.
this.exhibitorData = { 'property': changed };
I´ve been using this method for years and it doesnt work, angular is creating isolated scopes for each component or what?
EDIT: Lapsus, i need to share object and change properties of that object, not reasignin it.
Parent component
exhibitorData: object = {
exhibitor: {},
exhibitorId: ''
}
Use child component like that
<app-exhibitor-child [exhibitor-data]="exhibitorData"></app-exhibitor-child>
If I change exhibitorData.exhibitor in exhibitorChild component then I have same result on exhibitorData.exhibitor parent component.
this.exhibitodData.exhibitor = { 'property': 'changed' }
Sorry for lapsus
If you want to use one-way binding, you must not assign to this.exhibitorData
. You have to modify the object:
this.exhibitorData.property = changed;
It has nothing to do with Angular. It's how pass-by-value works in JavaScript.
Example:
function f1(obj) {
obj = { a: 1 };
}
function f2(obj) {
obj.a = 1;
}
const obj1 = { a: 0 };
const obj2 = { a: 0 };
f1(obj1);
f2(obj2);
console.log(obj1);
console.log(obj2);