Change variable set using ng-init in parent block












1














I need to use a variable as the condition in the ng-if directive, which is set in parent div using ng-init.



<div ng-repeat="item in list" ng-init="flag = false">
<div>
<div ng-if="item == a" ng-init="flag = true">
<!-- Some html code -->
</div>
<div ng-if="item == b" ng-init="flag = true">
<!-- Some html code -->
</div>
</div>
<div ng-if="flag == false">
<!-- Some html code -->
</div>
</div>


Can anyone tell me how to get this done?



Edit



I wanted to check whether each item in a list (list_a) is present in another list (list_b).



And for each item in list_a, display one div if present or display another div if not present in list_b.










share|improve this question




















  • 1




    The ng-init directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit. For more information, see AngularJS ng-init Directive API Reference. New AngularJS developers often do not realize that ng-repeat, ng-switch, ng-view, ng-include and ng-if all create new child scopes. See What are the nuances of scope inheritance in AngularJS?
    – georgeawg
    Nov 21 '18 at 19:56


















1














I need to use a variable as the condition in the ng-if directive, which is set in parent div using ng-init.



<div ng-repeat="item in list" ng-init="flag = false">
<div>
<div ng-if="item == a" ng-init="flag = true">
<!-- Some html code -->
</div>
<div ng-if="item == b" ng-init="flag = true">
<!-- Some html code -->
</div>
</div>
<div ng-if="flag == false">
<!-- Some html code -->
</div>
</div>


Can anyone tell me how to get this done?



Edit



I wanted to check whether each item in a list (list_a) is present in another list (list_b).



And for each item in list_a, display one div if present or display another div if not present in list_b.










share|improve this question




















  • 1




    The ng-init directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit. For more information, see AngularJS ng-init Directive API Reference. New AngularJS developers often do not realize that ng-repeat, ng-switch, ng-view, ng-include and ng-if all create new child scopes. See What are the nuances of scope inheritance in AngularJS?
    – georgeawg
    Nov 21 '18 at 19:56
















1












1








1







I need to use a variable as the condition in the ng-if directive, which is set in parent div using ng-init.



<div ng-repeat="item in list" ng-init="flag = false">
<div>
<div ng-if="item == a" ng-init="flag = true">
<!-- Some html code -->
</div>
<div ng-if="item == b" ng-init="flag = true">
<!-- Some html code -->
</div>
</div>
<div ng-if="flag == false">
<!-- Some html code -->
</div>
</div>


Can anyone tell me how to get this done?



Edit



I wanted to check whether each item in a list (list_a) is present in another list (list_b).



And for each item in list_a, display one div if present or display another div if not present in list_b.










share|improve this question















I need to use a variable as the condition in the ng-if directive, which is set in parent div using ng-init.



<div ng-repeat="item in list" ng-init="flag = false">
<div>
<div ng-if="item == a" ng-init="flag = true">
<!-- Some html code -->
</div>
<div ng-if="item == b" ng-init="flag = true">
<!-- Some html code -->
</div>
</div>
<div ng-if="flag == false">
<!-- Some html code -->
</div>
</div>


Can anyone tell me how to get this done?



Edit



I wanted to check whether each item in a list (list_a) is present in another list (list_b).



And for each item in list_a, display one div if present or display another div if not present in list_b.







angularjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 12:40

























asked Nov 21 '18 at 17:22









naseem

329




329








  • 1




    The ng-init directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit. For more information, see AngularJS ng-init Directive API Reference. New AngularJS developers often do not realize that ng-repeat, ng-switch, ng-view, ng-include and ng-if all create new child scopes. See What are the nuances of scope inheritance in AngularJS?
    – georgeawg
    Nov 21 '18 at 19:56
















  • 1




    The ng-init directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit. For more information, see AngularJS ng-init Directive API Reference. New AngularJS developers often do not realize that ng-repeat, ng-switch, ng-view, ng-include and ng-if all create new child scopes. See What are the nuances of scope inheritance in AngularJS?
    – georgeawg
    Nov 21 '18 at 19:56










1




1




The ng-init directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit. For more information, see AngularJS ng-init Directive API Reference. New AngularJS developers often do not realize that ng-repeat, ng-switch, ng-view, ng-include and ng-if all create new child scopes. See What are the nuances of scope inheritance in AngularJS?
– georgeawg
Nov 21 '18 at 19:56






The ng-init directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit. For more information, see AngularJS ng-init Directive API Reference. New AngularJS developers often do not realize that ng-repeat, ng-switch, ng-view, ng-include and ng-if all create new child scopes. See What are the nuances of scope inheritance in AngularJS?
– georgeawg
Nov 21 '18 at 19:56














2 Answers
2






active

oldest

votes


















1














forget about ng-init, create simple directive and include that wherever you need



<div ng-repeat="item in list">
<div>
<div ng-if="item == a">
<!-- Some html code for a -->
</div>
<div ng-if="item == b">
<!-- Some html code for b-->
</div>
</div>
<div ng-if="item != a && item != b">
<!-- Some html code -->
<!--or some directive-->
</div>
</div>





share|improve this answer





















  • I am using another list to store the list of items(a,b,..). In that case, what is the best way?
    – naseem
    Nov 21 '18 at 17:48










  • try to figure out yourself. try to understand what you want. angular gives you the way to write conditional tags like ng-if ng-show, based on them you could show/hide big html blocks
    – Dmitri Algazin
    Nov 23 '18 at 15:31



















0














To check whether the item, in list_a, is present in the list_b and display a block if present, I used



<div ng-if="list_b.indexOf(item)+1">



And to display another div if item is not present in the second list,



<div ng-if="!(list_b.indexOf(item)+1)">






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%2f53417513%2fchange-variable-set-using-ng-init-in-parent-block%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














    forget about ng-init, create simple directive and include that wherever you need



    <div ng-repeat="item in list">
    <div>
    <div ng-if="item == a">
    <!-- Some html code for a -->
    </div>
    <div ng-if="item == b">
    <!-- Some html code for b-->
    </div>
    </div>
    <div ng-if="item != a && item != b">
    <!-- Some html code -->
    <!--or some directive-->
    </div>
    </div>





    share|improve this answer





















    • I am using another list to store the list of items(a,b,..). In that case, what is the best way?
      – naseem
      Nov 21 '18 at 17:48










    • try to figure out yourself. try to understand what you want. angular gives you the way to write conditional tags like ng-if ng-show, based on them you could show/hide big html blocks
      – Dmitri Algazin
      Nov 23 '18 at 15:31
















    1














    forget about ng-init, create simple directive and include that wherever you need



    <div ng-repeat="item in list">
    <div>
    <div ng-if="item == a">
    <!-- Some html code for a -->
    </div>
    <div ng-if="item == b">
    <!-- Some html code for b-->
    </div>
    </div>
    <div ng-if="item != a && item != b">
    <!-- Some html code -->
    <!--or some directive-->
    </div>
    </div>





    share|improve this answer





















    • I am using another list to store the list of items(a,b,..). In that case, what is the best way?
      – naseem
      Nov 21 '18 at 17:48










    • try to figure out yourself. try to understand what you want. angular gives you the way to write conditional tags like ng-if ng-show, based on them you could show/hide big html blocks
      – Dmitri Algazin
      Nov 23 '18 at 15:31














    1












    1








    1






    forget about ng-init, create simple directive and include that wherever you need



    <div ng-repeat="item in list">
    <div>
    <div ng-if="item == a">
    <!-- Some html code for a -->
    </div>
    <div ng-if="item == b">
    <!-- Some html code for b-->
    </div>
    </div>
    <div ng-if="item != a && item != b">
    <!-- Some html code -->
    <!--or some directive-->
    </div>
    </div>





    share|improve this answer












    forget about ng-init, create simple directive and include that wherever you need



    <div ng-repeat="item in list">
    <div>
    <div ng-if="item == a">
    <!-- Some html code for a -->
    </div>
    <div ng-if="item == b">
    <!-- Some html code for b-->
    </div>
    </div>
    <div ng-if="item != a && item != b">
    <!-- Some html code -->
    <!--or some directive-->
    </div>
    </div>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 17:36









    Dmitri Algazin

    2,4021718




    2,4021718












    • I am using another list to store the list of items(a,b,..). In that case, what is the best way?
      – naseem
      Nov 21 '18 at 17:48










    • try to figure out yourself. try to understand what you want. angular gives you the way to write conditional tags like ng-if ng-show, based on them you could show/hide big html blocks
      – Dmitri Algazin
      Nov 23 '18 at 15:31


















    • I am using another list to store the list of items(a,b,..). In that case, what is the best way?
      – naseem
      Nov 21 '18 at 17:48










    • try to figure out yourself. try to understand what you want. angular gives you the way to write conditional tags like ng-if ng-show, based on them you could show/hide big html blocks
      – Dmitri Algazin
      Nov 23 '18 at 15:31
















    I am using another list to store the list of items(a,b,..). In that case, what is the best way?
    – naseem
    Nov 21 '18 at 17:48




    I am using another list to store the list of items(a,b,..). In that case, what is the best way?
    – naseem
    Nov 21 '18 at 17:48












    try to figure out yourself. try to understand what you want. angular gives you the way to write conditional tags like ng-if ng-show, based on them you could show/hide big html blocks
    – Dmitri Algazin
    Nov 23 '18 at 15:31




    try to figure out yourself. try to understand what you want. angular gives you the way to write conditional tags like ng-if ng-show, based on them you could show/hide big html blocks
    – Dmitri Algazin
    Nov 23 '18 at 15:31













    0














    To check whether the item, in list_a, is present in the list_b and display a block if present, I used



    <div ng-if="list_b.indexOf(item)+1">



    And to display another div if item is not present in the second list,



    <div ng-if="!(list_b.indexOf(item)+1)">






    share|improve this answer


























      0














      To check whether the item, in list_a, is present in the list_b and display a block if present, I used



      <div ng-if="list_b.indexOf(item)+1">



      And to display another div if item is not present in the second list,



      <div ng-if="!(list_b.indexOf(item)+1)">






      share|improve this answer
























        0












        0








        0






        To check whether the item, in list_a, is present in the list_b and display a block if present, I used



        <div ng-if="list_b.indexOf(item)+1">



        And to display another div if item is not present in the second list,



        <div ng-if="!(list_b.indexOf(item)+1)">






        share|improve this answer












        To check whether the item, in list_a, is present in the list_b and display a block if present, I used



        <div ng-if="list_b.indexOf(item)+1">



        And to display another div if item is not present in the second list,



        <div ng-if="!(list_b.indexOf(item)+1)">







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 12:19









        naseem

        329




        329






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53417513%2fchange-variable-set-using-ng-init-in-parent-block%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'