Consider the following scenario. I want to use three NameInputComponent
, which each wrap a text input, to construct a FullNamesComponent
:
- Two
NameInputComponent
s, for a pair of individual first names (firstName1
&firstName2
) - One
NameInputComponent
, for a shared last name (sharedLastName
)
The FullNamesComponent should expose two full names:
fullName1 = firstName1 + ' ' + sharedLastName
fullName2 = firstName2 + ' ' + sharedLastName
What would be the proper way to link these components together? I have tried using ngModel
, which allows me to bind the value of the input to a variable in the NameInputComponent
, but I'm at a loss for how to keep building up so I can do something with these values in a parent component. From what I understand, I need to use an EventEmitter
to make the value consumable from a parent, but I'm not sure how to go about this.
I should note this isn't being used as a form to be submitted. I'm looking to bind to the outputs and use them for other portions of the page, as well as be able to change the content of the inputs programmatically/via bindings to other sections of the page.
Here is what I have so far:
name-input.component.ts
import {Component} from 'angular2/core';
@Component({
selector: 'name-input',
template: `
<h2>{{inputValue}}</h2>
<input type="text" [(ngModel)]="inputValue">
`
})
export class NameInputComponent {
inputValue: string; // <= How do I detect changes to this?
}
full-names.component.ts
import {Component} from 'angular2/core';
@Component({
selector: 'full-names',
template: `
{{fullName1}} & {{fullName2}}
<name-input #firstName1></name-input>
<name-input #firstName2></name-input>
<name-input #sharedLastName></name-input>
`,
directives: [
NameInputComponent
],
providers: [
NameInputComponent
]
})
export class FullNamesComponent {
fullName1: string; // No idea how to link these
fullName2: string; // to values in components above
}