Child component has two data-bound input property, one of typo string
(inputVariable
) and another of type string[]
(inputArray
).
import {Component, Inject, Input} from 'angular2/core';
@Component(
{
selector: 'child-app',
template: `
{{inputVariable}}
<input type="button" (click)="onButtonOneClick()" value="changeFoo">
<ul>
<li *ngFor="#el of inputArray"> {{el}} </li>
</ul>
<input type="button" (click)="onButtonTwoClick()" value="ChangeLi">
`
})
export class ChildAppComponent {
@Input() inputVariable: string;
@Input() inputArray: string[];
onButtonOneClick() {
this.inputVariable = 'new string';
}
onButtonTwoClick() {
this.inputArray[0] = 'New element String';
}
}
Parent component has same properties and Initializes child components corresponding properties inside template ([inputArray]="inputArray" inputVariable="inputVariable"
)
import {Component} from 'angular2/core';
import {ChildAppComponent} from './childApp.component';
@Component({
selector: 'my-app',
template:
`
{{inputVariable}}
<input type="button" (click)="onButtonOneClick()" value="changeFoo">
<ul>
<li *ngFor="#el of inputArray"> {{el}} </li>
</ul>
<input type="button" (click)="onButtonTwoClick()" value="ChangeLi">
<hr>
<child-app [inputArray]="inputArray" inputVariable="inputVariable"> </child-app>
`,
directives: [ChildAppComponent]
})
export class AppComponent {
inputVariable: string = 'foo';
inputArray: string[] = ['one', 'two'];
onButtonOneClick() {
this.inputVariable = 'new string';
}
onButtonTwoClick() {
this.inputArray[0] = 'New element String';
}
}
Button clicks inside parent and child components changes values of corresponding property (buttonOne
-> inputVariable
& buttonTwo
-> inputArray
)
When click on second button (which changes string[]
property value) change happens both in parent and in child component
When click on first button (which changes string
property value) change only happens inside parent or child (respective to which component's button i clicked)
- Why behaviour is different based on property type ?
- How to have two way binding between child and parent component's
string
properties ?