NGXS use input value as id to filtering lazy selectorAngular2: pass boolean to @InputCan't bind to 'ngModel' since it isn't a known property of 'input'Angular2 input and highchartsPushing data to input() not getting detected: Angular 4Provide Component with a service instance that depends on component @InputError occurres on leaving and refreshing page, NGRX, AngularAsync pipe - data is undefined when reassignedIonic 3 pipe to filter list by comparing 2 arraysAngular 6,'Cannot read property of undefined' when calling function in serviceReuse selector in other selectors in NGXS

Why did my folder names end up like this, and how can I fix this using a script?

Given current technology, could TV display screens double as video camera sensors?

Billiard balls collision

Did anybody find out it was Anakin who blew up the command center?

To what extent are we obligated to continue to procreate beyond having two kids?

Why error propagation in CBC mode encryption affect two blocks?

Rent contract say that pets are not allowed. Possible repercussions if bringing the pet anyway?

Can an Arcane Focus be embedded in one's body?

Is this password scheme legit?

Disk usage of integer column vs boolean column in Postgres

Gambler coin problem: fair coin and two-headed coin

Is it possible to paint an object inside with one texture and outside with another?

How do you capitalize agile costs with less mature teams?

Redacting URLs as an email-phishing preventative?

Is it legal for source code containing undefined behavior to crash the compiler?

Can you board the plane when your passport is valid less than 3 months?

Thought experiment and possible contradiction between electromagnetism and special relativity

What is the name of my succulent?

How many lines of code does the original TeX contain?

Is it allowed to work ONLINE on F-1 visa?

Is first Ubuntu user root?

Why does a sticker slowly peel off, but if it is pulled quickly it tears?

Why is getting a PhD considered "financially irresponsible"?

What does it take for witness testimony to be believed?



NGXS use input value as id to filtering lazy selector


Angular2: pass boolean to @InputCan't bind to 'ngModel' since it isn't a known property of 'input'Angular2 input and highchartsPushing data to input() not getting detected: Angular 4Provide Component with a service instance that depends on component @InputError occurres on leaving and refreshing page, NGRX, AngularAsync pipe - data is undefined when reassignedIonic 3 pipe to filter list by comparing 2 arraysAngular 6,'Cannot read property of undefined' when calling function in serviceReuse selector in other selectors in NGXS






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a CommentBoxComponent that displays all the comments of the post in the containing PostComponent. I'm getting the id of the post from the containing parent component via an @Input field.

The idea of the CommentBoxComponent is to display a CommentComponent for each of the comments of the post.

I'm trying to achieve that by writing a lazy selector that will receive the id of the post and return the filtered comments.

The problem is that while I'm setting the lazy selector in the component's constructor, the input id has not been set yet.

If I try setting the lazy selector in ngOnChanges, it's already too late, and the selection has already taken place.



Here's the component's code:



@Component( ... )
export class CommentBoxComponent
@Input()
post: Post;

comments$: Observable<Comment[]>;

constructor(private store: Store)
let filter = filterFn => filterFn(this.post.id); // this.post is undefined at this stage

this.comments$ = this.store.select(CommentState.postComments).pipe(map(filter));




Here's the selector's code:



@Selector()
static postComments(state: CommentStateModel)
return (postId: string) =>
return state.comments.filter(comment => comments.postId === postId);
;



Any suggestions ?










share|improve this question






























    0















    I have a CommentBoxComponent that displays all the comments of the post in the containing PostComponent. I'm getting the id of the post from the containing parent component via an @Input field.

    The idea of the CommentBoxComponent is to display a CommentComponent for each of the comments of the post.

    I'm trying to achieve that by writing a lazy selector that will receive the id of the post and return the filtered comments.

    The problem is that while I'm setting the lazy selector in the component's constructor, the input id has not been set yet.

    If I try setting the lazy selector in ngOnChanges, it's already too late, and the selection has already taken place.



    Here's the component's code:



    @Component( ... )
    export class CommentBoxComponent
    @Input()
    post: Post;

    comments$: Observable<Comment[]>;

    constructor(private store: Store)
    let filter = filterFn => filterFn(this.post.id); // this.post is undefined at this stage

    this.comments$ = this.store.select(CommentState.postComments).pipe(map(filter));




    Here's the selector's code:



    @Selector()
    static postComments(state: CommentStateModel)
    return (postId: string) =>
    return state.comments.filter(comment => comments.postId === postId);
    ;



    Any suggestions ?










    share|improve this question


























      0












      0








      0








      I have a CommentBoxComponent that displays all the comments of the post in the containing PostComponent. I'm getting the id of the post from the containing parent component via an @Input field.

      The idea of the CommentBoxComponent is to display a CommentComponent for each of the comments of the post.

      I'm trying to achieve that by writing a lazy selector that will receive the id of the post and return the filtered comments.

      The problem is that while I'm setting the lazy selector in the component's constructor, the input id has not been set yet.

      If I try setting the lazy selector in ngOnChanges, it's already too late, and the selection has already taken place.



      Here's the component's code:



      @Component( ... )
      export class CommentBoxComponent
      @Input()
      post: Post;

      comments$: Observable<Comment[]>;

      constructor(private store: Store)
      let filter = filterFn => filterFn(this.post.id); // this.post is undefined at this stage

      this.comments$ = this.store.select(CommentState.postComments).pipe(map(filter));




      Here's the selector's code:



      @Selector()
      static postComments(state: CommentStateModel)
      return (postId: string) =>
      return state.comments.filter(comment => comments.postId === postId);
      ;



      Any suggestions ?










      share|improve this question














      I have a CommentBoxComponent that displays all the comments of the post in the containing PostComponent. I'm getting the id of the post from the containing parent component via an @Input field.

      The idea of the CommentBoxComponent is to display a CommentComponent for each of the comments of the post.

      I'm trying to achieve that by writing a lazy selector that will receive the id of the post and return the filtered comments.

      The problem is that while I'm setting the lazy selector in the component's constructor, the input id has not been set yet.

      If I try setting the lazy selector in ngOnChanges, it's already too late, and the selection has already taken place.



      Here's the component's code:



      @Component( ... )
      export class CommentBoxComponent
      @Input()
      post: Post;

      comments$: Observable<Comment[]>;

      constructor(private store: Store)
      let filter = filterFn => filterFn(this.post.id); // this.post is undefined at this stage

      this.comments$ = this.store.select(CommentState.postComments).pipe(map(filter));




      Here's the selector's code:



      @Selector()
      static postComments(state: CommentStateModel)
      return (postId: string) =>
      return state.comments.filter(comment => comments.postId === postId);
      ;



      Any suggestions ?







      angular ngxs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 20:00









      ethanfarethanfar

      3,23617 silver badges37 bronze badges




      3,23617 silver badges37 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          1















          You should just be able to use ngOnInit here to initialise comments$.



          When it is called, any @Input bindings will be completed so the value for this.post will be set.



          ngOnInit() 
          this.comments$ = this.store.select(CommentState.postComments)
          .pipe(
          map(fn => fn(this.post.id))
          );



          Lifecycle details here OnInit






          share|improve this answer

























          • That's the first thing I've tried, but even when the component is ready, it doesn't mean that the value, which is set asynchronously, is already set. I found the bug (see my answer above). +1 for the effort though !

            – ethanfar
            Mar 28 at 4:59






          • 1





            Ah I see, that's a little different to the way I'd used it so far. Glad you found the solution anyway!

            – Garth Mason
            Mar 28 at 5:44


















          0















          I found the bug, so I'm sharing here in case anyone's interested.

          I was wrong to assume that calling store.select after the construction doesn't work (it just hit me that there's no reason for it to be any different than calling it during ngOnChanges).



          It turns out that the selection was working just fine, but the reason I didn't get any results was that I didn't dispatch a FetchComments action before the selection.
          That means that I always got 0 commens back ...



          I just want to clarify that in such a case, the correct place to do the selection, is either in the setter of the @Input or during ngOnChanges where you can verify that the value of the input is valid.

          If you try to place the selection in ngOnInit, the @Input might not have been initialized yet in case of an asynchronoud initialization.






          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',
            autoActivateHeartbeat: false,
            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%2f55385542%2fngxs-use-input-value-as-id-to-filtering-lazy-selector%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1















            You should just be able to use ngOnInit here to initialise comments$.



            When it is called, any @Input bindings will be completed so the value for this.post will be set.



            ngOnInit() 
            this.comments$ = this.store.select(CommentState.postComments)
            .pipe(
            map(fn => fn(this.post.id))
            );



            Lifecycle details here OnInit






            share|improve this answer

























            • That's the first thing I've tried, but even when the component is ready, it doesn't mean that the value, which is set asynchronously, is already set. I found the bug (see my answer above). +1 for the effort though !

              – ethanfar
              Mar 28 at 4:59






            • 1





              Ah I see, that's a little different to the way I'd used it so far. Glad you found the solution anyway!

              – Garth Mason
              Mar 28 at 5:44















            1















            You should just be able to use ngOnInit here to initialise comments$.



            When it is called, any @Input bindings will be completed so the value for this.post will be set.



            ngOnInit() 
            this.comments$ = this.store.select(CommentState.postComments)
            .pipe(
            map(fn => fn(this.post.id))
            );



            Lifecycle details here OnInit






            share|improve this answer

























            • That's the first thing I've tried, but even when the component is ready, it doesn't mean that the value, which is set asynchronously, is already set. I found the bug (see my answer above). +1 for the effort though !

              – ethanfar
              Mar 28 at 4:59






            • 1





              Ah I see, that's a little different to the way I'd used it so far. Glad you found the solution anyway!

              – Garth Mason
              Mar 28 at 5:44













            1














            1










            1









            You should just be able to use ngOnInit here to initialise comments$.



            When it is called, any @Input bindings will be completed so the value for this.post will be set.



            ngOnInit() 
            this.comments$ = this.store.select(CommentState.postComments)
            .pipe(
            map(fn => fn(this.post.id))
            );



            Lifecycle details here OnInit






            share|improve this answer













            You should just be able to use ngOnInit here to initialise comments$.



            When it is called, any @Input bindings will be completed so the value for this.post will be set.



            ngOnInit() 
            this.comments$ = this.store.select(CommentState.postComments)
            .pipe(
            map(fn => fn(this.post.id))
            );



            Lifecycle details here OnInit







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 28 at 2:09









            Garth MasonGarth Mason

            3,6011 gold badge17 silver badges32 bronze badges




            3,6011 gold badge17 silver badges32 bronze badges















            • That's the first thing I've tried, but even when the component is ready, it doesn't mean that the value, which is set asynchronously, is already set. I found the bug (see my answer above). +1 for the effort though !

              – ethanfar
              Mar 28 at 4:59






            • 1





              Ah I see, that's a little different to the way I'd used it so far. Glad you found the solution anyway!

              – Garth Mason
              Mar 28 at 5:44

















            • That's the first thing I've tried, but even when the component is ready, it doesn't mean that the value, which is set asynchronously, is already set. I found the bug (see my answer above). +1 for the effort though !

              – ethanfar
              Mar 28 at 4:59






            • 1





              Ah I see, that's a little different to the way I'd used it so far. Glad you found the solution anyway!

              – Garth Mason
              Mar 28 at 5:44
















            That's the first thing I've tried, but even when the component is ready, it doesn't mean that the value, which is set asynchronously, is already set. I found the bug (see my answer above). +1 for the effort though !

            – ethanfar
            Mar 28 at 4:59





            That's the first thing I've tried, but even when the component is ready, it doesn't mean that the value, which is set asynchronously, is already set. I found the bug (see my answer above). +1 for the effort though !

            – ethanfar
            Mar 28 at 4:59




            1




            1





            Ah I see, that's a little different to the way I'd used it so far. Glad you found the solution anyway!

            – Garth Mason
            Mar 28 at 5:44





            Ah I see, that's a little different to the way I'd used it so far. Glad you found the solution anyway!

            – Garth Mason
            Mar 28 at 5:44













            0















            I found the bug, so I'm sharing here in case anyone's interested.

            I was wrong to assume that calling store.select after the construction doesn't work (it just hit me that there's no reason for it to be any different than calling it during ngOnChanges).



            It turns out that the selection was working just fine, but the reason I didn't get any results was that I didn't dispatch a FetchComments action before the selection.
            That means that I always got 0 commens back ...



            I just want to clarify that in such a case, the correct place to do the selection, is either in the setter of the @Input or during ngOnChanges where you can verify that the value of the input is valid.

            If you try to place the selection in ngOnInit, the @Input might not have been initialized yet in case of an asynchronoud initialization.






            share|improve this answer





























              0















              I found the bug, so I'm sharing here in case anyone's interested.

              I was wrong to assume that calling store.select after the construction doesn't work (it just hit me that there's no reason for it to be any different than calling it during ngOnChanges).



              It turns out that the selection was working just fine, but the reason I didn't get any results was that I didn't dispatch a FetchComments action before the selection.
              That means that I always got 0 commens back ...



              I just want to clarify that in such a case, the correct place to do the selection, is either in the setter of the @Input or during ngOnChanges where you can verify that the value of the input is valid.

              If you try to place the selection in ngOnInit, the @Input might not have been initialized yet in case of an asynchronoud initialization.






              share|improve this answer



























                0














                0










                0









                I found the bug, so I'm sharing here in case anyone's interested.

                I was wrong to assume that calling store.select after the construction doesn't work (it just hit me that there's no reason for it to be any different than calling it during ngOnChanges).



                It turns out that the selection was working just fine, but the reason I didn't get any results was that I didn't dispatch a FetchComments action before the selection.
                That means that I always got 0 commens back ...



                I just want to clarify that in such a case, the correct place to do the selection, is either in the setter of the @Input or during ngOnChanges where you can verify that the value of the input is valid.

                If you try to place the selection in ngOnInit, the @Input might not have been initialized yet in case of an asynchronoud initialization.






                share|improve this answer













                I found the bug, so I'm sharing here in case anyone's interested.

                I was wrong to assume that calling store.select after the construction doesn't work (it just hit me that there's no reason for it to be any different than calling it during ngOnChanges).



                It turns out that the selection was working just fine, but the reason I didn't get any results was that I didn't dispatch a FetchComments action before the selection.
                That means that I always got 0 commens back ...



                I just want to clarify that in such a case, the correct place to do the selection, is either in the setter of the @Input or during ngOnChanges where you can verify that the value of the input is valid.

                If you try to place the selection in ngOnInit, the @Input might not have been initialized yet in case of an asynchronoud initialization.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 28 at 5:03









                ethanfarethanfar

                3,23617 silver badges37 bronze badges




                3,23617 silver badges37 bronze badges






























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385542%2fngxs-use-input-value-as-id-to-filtering-lazy-selector%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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript