Separate two-way data binding in Angular
up vote
0
down vote
favorite
I have a custom table component that expects a model for some row selection actions that can be two-way bound like so:
<my-table [(selected)]="selectedRows"></my-table>
Optionally, I can also simply pass an item via one-way data binding if I don't care about the changes that happen to that model:
<my-table [selected]="selectedRows"></my-table>
If I want to not have a two-way bound data item, and instead want to have a data item I pass down to the table component via one-way data binding, and a handler/event emitter so that the syntax ends up not to different than this:
<my-table [selected]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
Is it possible with no, or minimal changes to the my-table component? Or do I have to create an @Output parameter on the my-table component to which I pass handleSelectedChanged($event)?
Thank you!
javascript angular typescript
add a comment |
up vote
0
down vote
favorite
I have a custom table component that expects a model for some row selection actions that can be two-way bound like so:
<my-table [(selected)]="selectedRows"></my-table>
Optionally, I can also simply pass an item via one-way data binding if I don't care about the changes that happen to that model:
<my-table [selected]="selectedRows"></my-table>
If I want to not have a two-way bound data item, and instead want to have a data item I pass down to the table component via one-way data binding, and a handler/event emitter so that the syntax ends up not to different than this:
<my-table [selected]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
Is it possible with no, or minimal changes to the my-table component? Or do I have to create an @Output parameter on the my-table component to which I pass handleSelectedChanged($event)?
Thank you!
javascript angular typescript
Can you showmy-tablecomponent code?
– Pankaj Parkar
Nov 9 at 23:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a custom table component that expects a model for some row selection actions that can be two-way bound like so:
<my-table [(selected)]="selectedRows"></my-table>
Optionally, I can also simply pass an item via one-way data binding if I don't care about the changes that happen to that model:
<my-table [selected]="selectedRows"></my-table>
If I want to not have a two-way bound data item, and instead want to have a data item I pass down to the table component via one-way data binding, and a handler/event emitter so that the syntax ends up not to different than this:
<my-table [selected]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
Is it possible with no, or minimal changes to the my-table component? Or do I have to create an @Output parameter on the my-table component to which I pass handleSelectedChanged($event)?
Thank you!
javascript angular typescript
I have a custom table component that expects a model for some row selection actions that can be two-way bound like so:
<my-table [(selected)]="selectedRows"></my-table>
Optionally, I can also simply pass an item via one-way data binding if I don't care about the changes that happen to that model:
<my-table [selected]="selectedRows"></my-table>
If I want to not have a two-way bound data item, and instead want to have a data item I pass down to the table component via one-way data binding, and a handler/event emitter so that the syntax ends up not to different than this:
<my-table [selected]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
Is it possible with no, or minimal changes to the my-table component? Or do I have to create an @Output parameter on the my-table component to which I pass handleSelectedChanged($event)?
Thank you!
javascript angular typescript
javascript angular typescript
asked Nov 9 at 22:49
Sammy I.
81811029
81811029
Can you showmy-tablecomponent code?
– Pankaj Parkar
Nov 9 at 23:00
add a comment |
Can you showmy-tablecomponent code?
– Pankaj Parkar
Nov 9 at 23:00
Can you show
my-table component code?– Pankaj Parkar
Nov 9 at 23:00
Can you show
my-table component code?– Pankaj Parkar
Nov 9 at 23:00
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
parent.component.html
<my-table [selected2]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
parent.component.ts
handleSelectedChanged(event)
// console.log(event)
this.selectedRows = event
my-table.component.ts
@Input() selected2: any;
@Output() selected: EventEmitter<any> = new EventEmitter();
OnCLCICK()
this.selected.emit('key':'value')
or :--
user.service.ts
@Output() selected: EventEmitter<any> = new EventEmitter();
setselected(data)
this.selected.emit(data);
getselected()
return this.selected;
my-table.component.ts
@Input() selected2: any;
constructor(
public user: Userservice
)
onclick()
this.user.setselected('key','val')
parent.component.html
<my-table [selected2]="selectedRows"></my-table>
parent.componnet.ts
constructor(
public user: Userservice
)
ngOnInit()
this.userService.getselected().subscribe((data) =>
this.getData(data);
);
getData(data)
console.log(data)
add a comment |
up vote
0
down vote
No you don't need to do any changes inside the my-table component table. Only when you want to use custom event to be emitted use (selectedChange) instead of (selected) that's it. I hope you already had Input binding selected and Output binding selectedChange in a place inside my-table component. Also selected change property binding is completely optional.
<my-table
[selected]="selectedRows"
(selectedChange)="handleSelectedChanged($event)">
</my-table>
If you're wondering how two way binding stuff needed to have Change suffix on event, because that's by design. For understanding it more clearly you can have a look at angular ngModel directive as well.
<input [ngModel]="myModel" (ngModelChange)="myModel = $event" />
// You can also do assignment by calling function
<input [ngModel]="myModel" (ngModelChange)="inpuChanged($event)" />
can be written as
<input [(ngModel)]="myModel" />
add a comment |
up vote
0
down vote
Your my-table.component.ts already has an @Input() and @Output() implemented i.e through selected and selectedChange() .
For a custom two way data binding , angular expects the event emitter and the variable to be something like this :
@Input() date : date ;
@Output() dateChange : EventEmitter<Date> = new EventEmitter<Date>();
So when you use [(date)] , you have a two way data binding created .
if you don't want to use two way data binding you can omit the () in the [(date)] and just use [date] , and it will still behave like a normal parent child component communication .
If you want to listen to the changes and do some action to the date variable value , then you can use (dateChange) event emitter and bind it with a function so that you can listen for the changes .
Now to answer your question whether you have to create a new @Output() in my-table.component.ts , well you don't have to create any thing or add any event emitters to bind your handleSelectedChanged($event) as an Event Emitter is implemented through selectedChange() . All you have to do now is :
<my-table [selected]="selectedRows" (selectedChange)='handleSelectedChanged($event)'></my-table>
So now you have selectedRows as an input and selectedChange as an output that emits an event if anything changes in selected and the event is passed through handleSelectedChanged() .
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
parent.component.html
<my-table [selected2]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
parent.component.ts
handleSelectedChanged(event)
// console.log(event)
this.selectedRows = event
my-table.component.ts
@Input() selected2: any;
@Output() selected: EventEmitter<any> = new EventEmitter();
OnCLCICK()
this.selected.emit('key':'value')
or :--
user.service.ts
@Output() selected: EventEmitter<any> = new EventEmitter();
setselected(data)
this.selected.emit(data);
getselected()
return this.selected;
my-table.component.ts
@Input() selected2: any;
constructor(
public user: Userservice
)
onclick()
this.user.setselected('key','val')
parent.component.html
<my-table [selected2]="selectedRows"></my-table>
parent.componnet.ts
constructor(
public user: Userservice
)
ngOnInit()
this.userService.getselected().subscribe((data) =>
this.getData(data);
);
getData(data)
console.log(data)
add a comment |
up vote
0
down vote
parent.component.html
<my-table [selected2]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
parent.component.ts
handleSelectedChanged(event)
// console.log(event)
this.selectedRows = event
my-table.component.ts
@Input() selected2: any;
@Output() selected: EventEmitter<any> = new EventEmitter();
OnCLCICK()
this.selected.emit('key':'value')
or :--
user.service.ts
@Output() selected: EventEmitter<any> = new EventEmitter();
setselected(data)
this.selected.emit(data);
getselected()
return this.selected;
my-table.component.ts
@Input() selected2: any;
constructor(
public user: Userservice
)
onclick()
this.user.setselected('key','val')
parent.component.html
<my-table [selected2]="selectedRows"></my-table>
parent.componnet.ts
constructor(
public user: Userservice
)
ngOnInit()
this.userService.getselected().subscribe((data) =>
this.getData(data);
);
getData(data)
console.log(data)
add a comment |
up vote
0
down vote
up vote
0
down vote
parent.component.html
<my-table [selected2]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
parent.component.ts
handleSelectedChanged(event)
// console.log(event)
this.selectedRows = event
my-table.component.ts
@Input() selected2: any;
@Output() selected: EventEmitter<any> = new EventEmitter();
OnCLCICK()
this.selected.emit('key':'value')
or :--
user.service.ts
@Output() selected: EventEmitter<any> = new EventEmitter();
setselected(data)
this.selected.emit(data);
getselected()
return this.selected;
my-table.component.ts
@Input() selected2: any;
constructor(
public user: Userservice
)
onclick()
this.user.setselected('key','val')
parent.component.html
<my-table [selected2]="selectedRows"></my-table>
parent.componnet.ts
constructor(
public user: Userservice
)
ngOnInit()
this.userService.getselected().subscribe((data) =>
this.getData(data);
);
getData(data)
console.log(data)
parent.component.html
<my-table [selected2]="selectedRows" (selected)="handleSelectedChanged($event)"></my-table>
parent.component.ts
handleSelectedChanged(event)
// console.log(event)
this.selectedRows = event
my-table.component.ts
@Input() selected2: any;
@Output() selected: EventEmitter<any> = new EventEmitter();
OnCLCICK()
this.selected.emit('key':'value')
or :--
user.service.ts
@Output() selected: EventEmitter<any> = new EventEmitter();
setselected(data)
this.selected.emit(data);
getselected()
return this.selected;
my-table.component.ts
@Input() selected2: any;
constructor(
public user: Userservice
)
onclick()
this.user.setselected('key','val')
parent.component.html
<my-table [selected2]="selectedRows"></my-table>
parent.componnet.ts
constructor(
public user: Userservice
)
ngOnInit()
this.userService.getselected().subscribe((data) =>
this.getData(data);
);
getData(data)
console.log(data)
edited Nov 9 at 23:10
answered Nov 9 at 22:57
Ahmad mnzr
767319
767319
add a comment |
add a comment |
up vote
0
down vote
No you don't need to do any changes inside the my-table component table. Only when you want to use custom event to be emitted use (selectedChange) instead of (selected) that's it. I hope you already had Input binding selected and Output binding selectedChange in a place inside my-table component. Also selected change property binding is completely optional.
<my-table
[selected]="selectedRows"
(selectedChange)="handleSelectedChanged($event)">
</my-table>
If you're wondering how two way binding stuff needed to have Change suffix on event, because that's by design. For understanding it more clearly you can have a look at angular ngModel directive as well.
<input [ngModel]="myModel" (ngModelChange)="myModel = $event" />
// You can also do assignment by calling function
<input [ngModel]="myModel" (ngModelChange)="inpuChanged($event)" />
can be written as
<input [(ngModel)]="myModel" />
add a comment |
up vote
0
down vote
No you don't need to do any changes inside the my-table component table. Only when you want to use custom event to be emitted use (selectedChange) instead of (selected) that's it. I hope you already had Input binding selected and Output binding selectedChange in a place inside my-table component. Also selected change property binding is completely optional.
<my-table
[selected]="selectedRows"
(selectedChange)="handleSelectedChanged($event)">
</my-table>
If you're wondering how two way binding stuff needed to have Change suffix on event, because that's by design. For understanding it more clearly you can have a look at angular ngModel directive as well.
<input [ngModel]="myModel" (ngModelChange)="myModel = $event" />
// You can also do assignment by calling function
<input [ngModel]="myModel" (ngModelChange)="inpuChanged($event)" />
can be written as
<input [(ngModel)]="myModel" />
add a comment |
up vote
0
down vote
up vote
0
down vote
No you don't need to do any changes inside the my-table component table. Only when you want to use custom event to be emitted use (selectedChange) instead of (selected) that's it. I hope you already had Input binding selected and Output binding selectedChange in a place inside my-table component. Also selected change property binding is completely optional.
<my-table
[selected]="selectedRows"
(selectedChange)="handleSelectedChanged($event)">
</my-table>
If you're wondering how two way binding stuff needed to have Change suffix on event, because that's by design. For understanding it more clearly you can have a look at angular ngModel directive as well.
<input [ngModel]="myModel" (ngModelChange)="myModel = $event" />
// You can also do assignment by calling function
<input [ngModel]="myModel" (ngModelChange)="inpuChanged($event)" />
can be written as
<input [(ngModel)]="myModel" />
No you don't need to do any changes inside the my-table component table. Only when you want to use custom event to be emitted use (selectedChange) instead of (selected) that's it. I hope you already had Input binding selected and Output binding selectedChange in a place inside my-table component. Also selected change property binding is completely optional.
<my-table
[selected]="selectedRows"
(selectedChange)="handleSelectedChanged($event)">
</my-table>
If you're wondering how two way binding stuff needed to have Change suffix on event, because that's by design. For understanding it more clearly you can have a look at angular ngModel directive as well.
<input [ngModel]="myModel" (ngModelChange)="myModel = $event" />
// You can also do assignment by calling function
<input [ngModel]="myModel" (ngModelChange)="inpuChanged($event)" />
can be written as
<input [(ngModel)]="myModel" />
edited Nov 9 at 23:19
answered Nov 9 at 23:06
Pankaj Parkar
111k15157232
111k15157232
add a comment |
add a comment |
up vote
0
down vote
Your my-table.component.ts already has an @Input() and @Output() implemented i.e through selected and selectedChange() .
For a custom two way data binding , angular expects the event emitter and the variable to be something like this :
@Input() date : date ;
@Output() dateChange : EventEmitter<Date> = new EventEmitter<Date>();
So when you use [(date)] , you have a two way data binding created .
if you don't want to use two way data binding you can omit the () in the [(date)] and just use [date] , and it will still behave like a normal parent child component communication .
If you want to listen to the changes and do some action to the date variable value , then you can use (dateChange) event emitter and bind it with a function so that you can listen for the changes .
Now to answer your question whether you have to create a new @Output() in my-table.component.ts , well you don't have to create any thing or add any event emitters to bind your handleSelectedChanged($event) as an Event Emitter is implemented through selectedChange() . All you have to do now is :
<my-table [selected]="selectedRows" (selectedChange)='handleSelectedChanged($event)'></my-table>
So now you have selectedRows as an input and selectedChange as an output that emits an event if anything changes in selected and the event is passed through handleSelectedChanged() .
add a comment |
up vote
0
down vote
Your my-table.component.ts already has an @Input() and @Output() implemented i.e through selected and selectedChange() .
For a custom two way data binding , angular expects the event emitter and the variable to be something like this :
@Input() date : date ;
@Output() dateChange : EventEmitter<Date> = new EventEmitter<Date>();
So when you use [(date)] , you have a two way data binding created .
if you don't want to use two way data binding you can omit the () in the [(date)] and just use [date] , and it will still behave like a normal parent child component communication .
If you want to listen to the changes and do some action to the date variable value , then you can use (dateChange) event emitter and bind it with a function so that you can listen for the changes .
Now to answer your question whether you have to create a new @Output() in my-table.component.ts , well you don't have to create any thing or add any event emitters to bind your handleSelectedChanged($event) as an Event Emitter is implemented through selectedChange() . All you have to do now is :
<my-table [selected]="selectedRows" (selectedChange)='handleSelectedChanged($event)'></my-table>
So now you have selectedRows as an input and selectedChange as an output that emits an event if anything changes in selected and the event is passed through handleSelectedChanged() .
add a comment |
up vote
0
down vote
up vote
0
down vote
Your my-table.component.ts already has an @Input() and @Output() implemented i.e through selected and selectedChange() .
For a custom two way data binding , angular expects the event emitter and the variable to be something like this :
@Input() date : date ;
@Output() dateChange : EventEmitter<Date> = new EventEmitter<Date>();
So when you use [(date)] , you have a two way data binding created .
if you don't want to use two way data binding you can omit the () in the [(date)] and just use [date] , and it will still behave like a normal parent child component communication .
If you want to listen to the changes and do some action to the date variable value , then you can use (dateChange) event emitter and bind it with a function so that you can listen for the changes .
Now to answer your question whether you have to create a new @Output() in my-table.component.ts , well you don't have to create any thing or add any event emitters to bind your handleSelectedChanged($event) as an Event Emitter is implemented through selectedChange() . All you have to do now is :
<my-table [selected]="selectedRows" (selectedChange)='handleSelectedChanged($event)'></my-table>
So now you have selectedRows as an input and selectedChange as an output that emits an event if anything changes in selected and the event is passed through handleSelectedChanged() .
Your my-table.component.ts already has an @Input() and @Output() implemented i.e through selected and selectedChange() .
For a custom two way data binding , angular expects the event emitter and the variable to be something like this :
@Input() date : date ;
@Output() dateChange : EventEmitter<Date> = new EventEmitter<Date>();
So when you use [(date)] , you have a two way data binding created .
if you don't want to use two way data binding you can omit the () in the [(date)] and just use [date] , and it will still behave like a normal parent child component communication .
If you want to listen to the changes and do some action to the date variable value , then you can use (dateChange) event emitter and bind it with a function so that you can listen for the changes .
Now to answer your question whether you have to create a new @Output() in my-table.component.ts , well you don't have to create any thing or add any event emitters to bind your handleSelectedChanged($event) as an Event Emitter is implemented through selectedChange() . All you have to do now is :
<my-table [selected]="selectedRows" (selectedChange)='handleSelectedChanged($event)'></my-table>
So now you have selectedRows as an input and selectedChange as an output that emits an event if anything changes in selected and the event is passed through handleSelectedChanged() .
answered Nov 10 at 4:54
CruelEngine
9141918
9141918
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234264%2fseparate-two-way-data-binding-in-angular%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show
my-tablecomponent code?– Pankaj Parkar
Nov 9 at 23:00