Angular *ngIf variable with async pipe multiple conditions










2















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.










share|improve this question


























    2















    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.










    share|improve this question
























      2












      2








      2








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 15 '18 at 10:16









      VitalyVitaly

      5316




      5316






















          2 Answers
          2






          active

          oldest

          votes


















          3














          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.






          share|improve this answer




















          • 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


















          0














          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.






          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%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









            3














            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.






            share|improve this answer




















            • 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















            3














            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.






            share|improve this answer




















            • 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













            3












            3








            3







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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












            • 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













            0














            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.






            share|improve this answer



























              0














              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.






              share|improve this answer

























                0












                0








                0







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 3:06









                KeegoKeego

                1,316175




                1,316175



























                    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%2f49296784%2fangular-ngif-variable-with-async-pipe-multiple-conditions%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







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3