I am trying to make something work on angular2 and I am unable to find something about this behavior.
I have an application that implements a custom component like this one :
import {Component,Input} from 'angular2/core'
@Component({
selector:'my-comp',
template:`<input type="text" style="text-align:center; [(ngModel)]="inputText"> <p>{{inputText}}</p>`
})
export class MyComp{
@Input() inputText : string;
}
And I am trying to do a bidirectional databinding on my inputText
variable from my component like this:
<my-comp [(inputText)]="testString"></my-comp>
Where the testString
is a variable defined in the MyApp.ts
which contains a string. I want my testString
variable to be modified when my inputText
is modified by the user.
Here is a Plunker with a simple sample code : https://plnkr.co/edit/zQiCQ3hxSSjCmhWJMJph?p=preview
Is there a way to make this works simply ? Do I have to implements an Angular2 class on my custom components and overload functions in order to make this works like an ngModel
? Do i necessarily have to create a inputTextChanged
variable of EventEmitter
type that emit my data when it's changed and do something like this :
<my-comp [inputText]="testString" (inputTextChanged)="testString = $event;"></my-comp>
Thank you in advance.
[(something)]
is just syntactic sugar for one property binding and one event binding. So Angular does not treat[(something)]
as one two-way databinding, but rather as two one-way data bindings. – Trenatrenail