I see in primeng components the use something like ngModel (two-way data binding) style for some property like this
[(selection)]="selectedItem";
it 's look like
@Input() selection;
@Output() selection:EventEmitter<any> = new EventEmitter<any>();
how I can implement something like this and is it possible to do for more than single property like
<component-a [(selection)]="selectedItem" [(data)]="selectedData"></component-a>
For two-way data binding, declare a private property and access its value using get and set methods in the component class. Then, assign that property to [(ngModel)] . For example, declare a private property with a get and set method, as shown below. Now, assign userName to [(ngModel)] in the template.
Two-way data binding can be achieved using a ngModel directive in Angular. The below syntax shows the data binding using (ngModel), which is basically the combination of both the square brackets of property binding and parentheses of the event binding.
AngularJS provides two types of Data Binding: One-way data binding. Two-way data binding.
Property binding in Angular helps you set values for properties of HTML elements or directives. Use property binding to do things such as toggle button features, set paths programmatically, and share values between components.
Angular docs
<app-sizer
[(size)]="fontSizePx">
</app-sizer>
The two-way binding syntax is really just syntactic sugar for a property binding and an event binding. Angular desugars the binding into this:
<app-sizer
[size]="fontSizePx"
(sizeChange)="fontSizePx=$event">
</app-sizer>
To create two-way binding for property selection
use:
@Input() selection;
// You have to follow this convention (For Output property)
// i.e. <propertyName><Change> like selectionChange
@Output() selectionChange:EventEmitter<any> = new EventEmitter<any>();
And to change selection in component as shown below:
changeSelection(newSelection)
{
this.selection = newSelection;
// emit change event to parent component
this.selectionChange.emit(this.selection);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With