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!










share|improve this question





















  • Can you show my-table component code?
    – Pankaj Parkar
    Nov 9 at 23:00














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!










share|improve this question





















  • Can you show my-table component code?
    – Pankaj Parkar
    Nov 9 at 23:00












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!










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 22:49









Sammy I.

81811029




81811029











  • 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















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












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)






share|improve this answer





























    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" />





    share|improve this answer





























      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() .






      share|improve this answer




















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        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

























        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)






        share|improve this answer


























          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)






          share|improve this answer
























            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)






            share|improve this answer














            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)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 23:10

























            answered Nov 9 at 22:57









            Ahmad mnzr

            767319




            767319






















                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" />





                share|improve this answer


























                  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" />





                  share|improve this answer
























                    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" />





                    share|improve this answer














                    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" />






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 9 at 23:19

























                    answered Nov 9 at 23:06









                    Pankaj Parkar

                    111k15157232




                    111k15157232




















                        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() .






                        share|improve this answer
























                          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() .






                          share|improve this answer






















                            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() .






                            share|improve this answer












                            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() .







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 10 at 4:54









                            CruelEngine

                            9141918




                            9141918



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Ruanda

                                Makov (Slowakei)

                                Kleinkühnau