0

I'm trying to do two way databinding using the banana in box sytax, and I need to pass a value down to a component (compone) then to another component (comptwo) where it will be edited.

My expectation was that the binding will enable the changes to be reflected in the app.component. However, I'm unable to do this.

I've used the example from this stackoverflow response, although it is not the voted answer to the question. I've seen this being represented in other blogs.

I've created a stackblitz of my issue. I'm just after help and possible explanation of what I'm doing wrong ?

Edited : to include code snippets from stackblitz:

App.Component.ts

export class AppComponent  {
  public _Name = 'Angular';  
}

app.component.html

From app.component.html : {{_Name}}

compone.component.ts

...
public _Name:string = "";
@Output() NameChange:EventEmitter<string> = new EventEmitter<string>();
@Input()
set Name(value:string)
{

  this._Name = value;
  this.NameChange.emit(value);

}
get Name():string
{
  return this._Name;

}
...

compone.component.html

...
<p>
compone works!
<app-comptwo [(Name)]="_Name"></app-comptwo>
{{_Name}}
</p>
...

comptwo.component.ts

...
public _Name:string = "";
@Output() NameChange:EventEmitter<string> = new EventEmitter<string>();
@Input()
set Name(value:string)
{

  this._Name = value;
  this.NameChange.emit(value);

}
get Name():string
{
  return this._Name;

}
...

comptwo.component.html

...
<p>
comptwo works! <input [(ngModel)]="_Name">
{{_Name}}
</p>
...

As it can be seen above, app.component is where the field originates from. It is passed into compone, and then into comptwo. Comptwo component is where the field is being modified via an input tag.

user3836415
  • 952
  • 1
  • 10
  • 25
  • 2
    Although you provided stackblitz which is very good also please provide some code here – Reza Mar 27 '19 at 14:18
  • Hi good point, I avoided adding the code here because at times StackOverflow complains that there is more code than an explanation. – user3836415 Mar 28 '19 at 00:35

1 Answers1

2

If you are using setter/getter, you have to bind events to them (in this case Name, not directly to model field (_Name). Getters/setters wont be called otherwise if you are binding to private _Field as you are literally bypassing setters.

https://stackblitz.com/edit/angular-awlpfh

Result of using propert bindings:

enter image description here

Edited:

compone.component.html is altered from :

<app-comptwo [(Name)]="_Name"></app-comptwo>

to this:

<app-comptwo [(Name)]="Name"></app-comptwo>
user3836415
  • 952
  • 1
  • 10
  • 25
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • what file contains the changed code? i am curious about how this works. – omikes Mar 27 '19 at 15:45
  • template files. – Antoniossss Mar 27 '19 at 15:45
  • ah, ok. i see the difference in `compone.component.html` and `comptwo.component.html`. thank you! – omikes Mar 27 '19 at 16:05
  • Since it solves the issue, you can mark it as acceppted. – Antoniossss Mar 27 '19 at 18:40
  • Hi @Antoniossss thanks for your help with this. So as I understand the in Compone template you are using the Input set/get () from Compone instead of the model field. Do you know of any resources that goes in depth into how this is all working - I'm still struggling to understand the roles of the getter/setter and EventEmitter. – user3836415 Mar 28 '19 at 00:31
  • Hi I hope you don't mind I've updated your response with the code snippet of the changes. This was based on the comment by @RezaRahmati on the question. – user3836415 Mar 28 '19 at 00:52
  • Angular tutorials are great explanation of how binding works. This is kind of straight forward - you wrote your event broadcast logic in setter methods, but you bypasses it setting value of field directly. Im glad you are on the right track now. – Antoniossss Mar 28 '19 at 07:03