Angular *ngIf variable with async pipe multiple conditions
There's quite good doc of using *ngIf in Angular: https://angular.io/api/common/NgIf
But, is that possible to have *ngIf async variable and multiple checks on that?
Something like:
<div *ngIf="users$ | async as users && users.length > 1">
...
</div>
Of course, it's possible to use nested *ngIf, like:
<div *ngIf="users$ | async as users">
<ng-container *ngIf="users.length > 1">
...
</ng-container>
</div>
but it'd be really nice to use only one container, not two.
angular angular-ng-if
add a comment |
There's quite good doc of using *ngIf in Angular: https://angular.io/api/common/NgIf
But, is that possible to have *ngIf async variable and multiple checks on that?
Something like:
<div *ngIf="users$ | async as users && users.length > 1">
...
</div>
Of course, it's possible to use nested *ngIf, like:
<div *ngIf="users$ | async as users">
<ng-container *ngIf="users.length > 1">
...
</ng-container>
</div>
but it'd be really nice to use only one container, not two.
angular angular-ng-if
add a comment |
There's quite good doc of using *ngIf in Angular: https://angular.io/api/common/NgIf
But, is that possible to have *ngIf async variable and multiple checks on that?
Something like:
<div *ngIf="users$ | async as users && users.length > 1">
...
</div>
Of course, it's possible to use nested *ngIf, like:
<div *ngIf="users$ | async as users">
<ng-container *ngIf="users.length > 1">
...
</ng-container>
</div>
but it'd be really nice to use only one container, not two.
angular angular-ng-if
There's quite good doc of using *ngIf in Angular: https://angular.io/api/common/NgIf
But, is that possible to have *ngIf async variable and multiple checks on that?
Something like:
<div *ngIf="users$ | async as users && users.length > 1">
...
</div>
Of course, it's possible to use nested *ngIf, like:
<div *ngIf="users$ | async as users">
<ng-container *ngIf="users.length > 1">
...
</ng-container>
</div>
but it'd be really nice to use only one container, not two.
angular angular-ng-if
angular angular-ng-if
asked Mar 15 '18 at 10:16
VitalyVitaly
5316
5316
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Simply do it like this
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1">...</div>
For "more complex" scenario do the following
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">...</div>
Edit: Previous wouldn't work since you cannot use *ngFor
and *ngIf
without using ng-template. You would do it like that for instance
<ng-template ngFor let-user [ngForOf]="users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">
<div> user </div>
</ng-template>
Here is a stackblitz.
1
yep, that's good suggestion, but what about more complex conditions: <div *ngIf="users$ | async as users && users.length > 1 && users.length < 5"> <div *ngFor="let user of users">...
– Vitaly
Mar 15 '18 at 10:18
what kind of ? you can have multiple statements like this. in one *ngIf, just chain em.
– alsami
Mar 15 '18 at 10:19
sorry, pressed enter too soon)
– Vitaly
Mar 15 '18 at 10:21
in general, i want to keep local variable for further usage with only one subscription and your solution has no variable and multiple subsriptions
– Vitaly
Mar 15 '18 at 10:21
3
Has anyone actually tested this? (I haven't but) In my understanding of Angular, you cannot use multiple structural directives on the same element. So *ngIf and *ngFor normally cannot be used together on the same element!
– AlainD.
Oct 11 '18 at 9:57
|
show 8 more comments
I hit the same issue of needing an *ngIf + async variable with multiple checks.
This ended up working well for me.
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async) as users"> ... </div>
or if you prefer
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async); let users"> ... </div>
Explanation
Since the result of the if expression is assigned to the local variable you specify, simply ending your check with ... && (users$ | async) as users
allows you to specify multiple conditions and specify what value you want the local variable to hold when all your conditions succeed.
Note
I was initially worried that using multiple async
pipes in the same expression may create multiple subscriptions, but after some light testing (I could be wrong) it seems like only one subscription is actually made.
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%2f49296784%2fangular-ngif-variable-with-async-pipe-multiple-conditions%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
Simply do it like this
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1">...</div>
For "more complex" scenario do the following
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">...</div>
Edit: Previous wouldn't work since you cannot use *ngFor
and *ngIf
without using ng-template. You would do it like that for instance
<ng-template ngFor let-user [ngForOf]="users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">
<div> user </div>
</ng-template>
Here is a stackblitz.
1
yep, that's good suggestion, but what about more complex conditions: <div *ngIf="users$ | async as users && users.length > 1 && users.length < 5"> <div *ngFor="let user of users">...
– Vitaly
Mar 15 '18 at 10:18
what kind of ? you can have multiple statements like this. in one *ngIf, just chain em.
– alsami
Mar 15 '18 at 10:19
sorry, pressed enter too soon)
– Vitaly
Mar 15 '18 at 10:21
in general, i want to keep local variable for further usage with only one subscription and your solution has no variable and multiple subsriptions
– Vitaly
Mar 15 '18 at 10:21
3
Has anyone actually tested this? (I haven't but) In my understanding of Angular, you cannot use multiple structural directives on the same element. So *ngIf and *ngFor normally cannot be used together on the same element!
– AlainD.
Oct 11 '18 at 9:57
|
show 8 more comments
Simply do it like this
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1">...</div>
For "more complex" scenario do the following
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">...</div>
Edit: Previous wouldn't work since you cannot use *ngFor
and *ngIf
without using ng-template. You would do it like that for instance
<ng-template ngFor let-user [ngForOf]="users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">
<div> user </div>
</ng-template>
Here is a stackblitz.
1
yep, that's good suggestion, but what about more complex conditions: <div *ngIf="users$ | async as users && users.length > 1 && users.length < 5"> <div *ngFor="let user of users">...
– Vitaly
Mar 15 '18 at 10:18
what kind of ? you can have multiple statements like this. in one *ngIf, just chain em.
– alsami
Mar 15 '18 at 10:19
sorry, pressed enter too soon)
– Vitaly
Mar 15 '18 at 10:21
in general, i want to keep local variable for further usage with only one subscription and your solution has no variable and multiple subsriptions
– Vitaly
Mar 15 '18 at 10:21
3
Has anyone actually tested this? (I haven't but) In my understanding of Angular, you cannot use multiple structural directives on the same element. So *ngIf and *ngFor normally cannot be used together on the same element!
– AlainD.
Oct 11 '18 at 9:57
|
show 8 more comments
Simply do it like this
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1">...</div>
For "more complex" scenario do the following
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">...</div>
Edit: Previous wouldn't work since you cannot use *ngFor
and *ngIf
without using ng-template. You would do it like that for instance
<ng-template ngFor let-user [ngForOf]="users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">
<div> user </div>
</ng-template>
Here is a stackblitz.
Simply do it like this
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1">...</div>
For "more complex" scenario do the following
<div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">...</div>
Edit: Previous wouldn't work since you cannot use *ngFor
and *ngIf
without using ng-template. You would do it like that for instance
<ng-template ngFor let-user [ngForOf]="users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">
<div> user </div>
</ng-template>
Here is a stackblitz.
edited Oct 11 '18 at 10:10
answered Mar 15 '18 at 10:17
alsamialsami
2,1502615
2,1502615
1
yep, that's good suggestion, but what about more complex conditions: <div *ngIf="users$ | async as users && users.length > 1 && users.length < 5"> <div *ngFor="let user of users">...
– Vitaly
Mar 15 '18 at 10:18
what kind of ? you can have multiple statements like this. in one *ngIf, just chain em.
– alsami
Mar 15 '18 at 10:19
sorry, pressed enter too soon)
– Vitaly
Mar 15 '18 at 10:21
in general, i want to keep local variable for further usage with only one subscription and your solution has no variable and multiple subsriptions
– Vitaly
Mar 15 '18 at 10:21
3
Has anyone actually tested this? (I haven't but) In my understanding of Angular, you cannot use multiple structural directives on the same element. So *ngIf and *ngFor normally cannot be used together on the same element!
– AlainD.
Oct 11 '18 at 9:57
|
show 8 more comments
1
yep, that's good suggestion, but what about more complex conditions: <div *ngIf="users$ | async as users && users.length > 1 && users.length < 5"> <div *ngFor="let user of users">...
– Vitaly
Mar 15 '18 at 10:18
what kind of ? you can have multiple statements like this. in one *ngIf, just chain em.
– alsami
Mar 15 '18 at 10:19
sorry, pressed enter too soon)
– Vitaly
Mar 15 '18 at 10:21
in general, i want to keep local variable for further usage with only one subscription and your solution has no variable and multiple subsriptions
– Vitaly
Mar 15 '18 at 10:21
3
Has anyone actually tested this? (I haven't but) In my understanding of Angular, you cannot use multiple structural directives on the same element. So *ngIf and *ngFor normally cannot be used together on the same element!
– AlainD.
Oct 11 '18 at 9:57
1
1
yep, that's good suggestion, but what about more complex conditions: <div *ngIf="users$ | async as users && users.length > 1 && users.length < 5"> <div *ngFor="let user of users">...
– Vitaly
Mar 15 '18 at 10:18
yep, that's good suggestion, but what about more complex conditions: <div *ngIf="users$ | async as users && users.length > 1 && users.length < 5"> <div *ngFor="let user of users">...
– Vitaly
Mar 15 '18 at 10:18
what kind of ? you can have multiple statements like this. in one *ngIf, just chain em.
– alsami
Mar 15 '18 at 10:19
what kind of ? you can have multiple statements like this. in one *ngIf, just chain em.
– alsami
Mar 15 '18 at 10:19
sorry, pressed enter too soon)
– Vitaly
Mar 15 '18 at 10:21
sorry, pressed enter too soon)
– Vitaly
Mar 15 '18 at 10:21
in general, i want to keep local variable for further usage with only one subscription and your solution has no variable and multiple subsriptions
– Vitaly
Mar 15 '18 at 10:21
in general, i want to keep local variable for further usage with only one subscription and your solution has no variable and multiple subsriptions
– Vitaly
Mar 15 '18 at 10:21
3
3
Has anyone actually tested this? (I haven't but) In my understanding of Angular, you cannot use multiple structural directives on the same element. So *ngIf and *ngFor normally cannot be used together on the same element!
– AlainD.
Oct 11 '18 at 9:57
Has anyone actually tested this? (I haven't but) In my understanding of Angular, you cannot use multiple structural directives on the same element. So *ngIf and *ngFor normally cannot be used together on the same element!
– AlainD.
Oct 11 '18 at 9:57
|
show 8 more comments
I hit the same issue of needing an *ngIf + async variable with multiple checks.
This ended up working well for me.
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async) as users"> ... </div>
or if you prefer
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async); let users"> ... </div>
Explanation
Since the result of the if expression is assigned to the local variable you specify, simply ending your check with ... && (users$ | async) as users
allows you to specify multiple conditions and specify what value you want the local variable to hold when all your conditions succeed.
Note
I was initially worried that using multiple async
pipes in the same expression may create multiple subscriptions, but after some light testing (I could be wrong) it seems like only one subscription is actually made.
add a comment |
I hit the same issue of needing an *ngIf + async variable with multiple checks.
This ended up working well for me.
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async) as users"> ... </div>
or if you prefer
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async); let users"> ... </div>
Explanation
Since the result of the if expression is assigned to the local variable you specify, simply ending your check with ... && (users$ | async) as users
allows you to specify multiple conditions and specify what value you want the local variable to hold when all your conditions succeed.
Note
I was initially worried that using multiple async
pipes in the same expression may create multiple subscriptions, but after some light testing (I could be wrong) it seems like only one subscription is actually made.
add a comment |
I hit the same issue of needing an *ngIf + async variable with multiple checks.
This ended up working well for me.
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async) as users"> ... </div>
or if you prefer
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async); let users"> ... </div>
Explanation
Since the result of the if expression is assigned to the local variable you specify, simply ending your check with ... && (users$ | async) as users
allows you to specify multiple conditions and specify what value you want the local variable to hold when all your conditions succeed.
Note
I was initially worried that using multiple async
pipes in the same expression may create multiple subscriptions, but after some light testing (I could be wrong) it seems like only one subscription is actually made.
I hit the same issue of needing an *ngIf + async variable with multiple checks.
This ended up working well for me.
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async) as users"> ... </div>
or if you prefer
<div *ngIf="(users$ | async)?.length > 0 && (users$ | async); let users"> ... </div>
Explanation
Since the result of the if expression is assigned to the local variable you specify, simply ending your check with ... && (users$ | async) as users
allows you to specify multiple conditions and specify what value you want the local variable to hold when all your conditions succeed.
Note
I was initially worried that using multiple async
pipes in the same expression may create multiple subscriptions, but after some light testing (I could be wrong) it seems like only one subscription is actually made.
answered Nov 13 '18 at 3:06
KeegoKeego
1,316175
1,316175
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%2f49296784%2fangular-ngif-variable-with-async-pipe-multiple-conditions%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