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;
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
add a comment |
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
add a comment |
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
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
angular ngxs
asked Mar 27 at 20:00
ethanfarethanfar
3,23617 silver badges37 bronze badges
3,23617 silver badges37 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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.
add a comment |
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
);
);
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%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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 28 at 5:03
ethanfarethanfar
3,23617 silver badges37 bronze badges
3,23617 silver badges37 bronze badges
add a comment |
add a comment |
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.
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%2f55385542%2fngxs-use-input-value-as-id-to-filtering-lazy-selector%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