How to use conditionals without attaching it to an element?










2















In vue, we have got directives v-if which needs to be attached to element.



Is there a way to use conditionals without attaching them to anything like the mustachejs way?



I am looping through an array of words and it is adding div's in every word which is annoying



Here is my template



<div v-for="(str, index) in reference" :key="index">
<div v-if="patternIncluded(str)">
<input type="text" v-model="remarks[index]">
</div>
<div v-else>
str
</div>
</div>


I would be nice if i could do it like this:|



if true
something goes here
else
other thing goes here
if









share|improve this question
























  • You'd probably use a ternary in an expression, but we probably need to see some code to help you

    – George Jempty
    Nov 14 '18 at 13:09











  • @GeorgeJempty i have updated my question

    – Raaz
    Nov 14 '18 at 13:10











  • I'm not sure if this is what you want to achieve, that's why I'm posting this as an comment: jsfiddle.net/edsLrjmu/1 (Example contains two possible solutions)

    – Jns
    Nov 14 '18 at 13:46















2















In vue, we have got directives v-if which needs to be attached to element.



Is there a way to use conditionals without attaching them to anything like the mustachejs way?



I am looping through an array of words and it is adding div's in every word which is annoying



Here is my template



<div v-for="(str, index) in reference" :key="index">
<div v-if="patternIncluded(str)">
<input type="text" v-model="remarks[index]">
</div>
<div v-else>
str
</div>
</div>


I would be nice if i could do it like this:|



if true
something goes here
else
other thing goes here
if









share|improve this question
























  • You'd probably use a ternary in an expression, but we probably need to see some code to help you

    – George Jempty
    Nov 14 '18 at 13:09











  • @GeorgeJempty i have updated my question

    – Raaz
    Nov 14 '18 at 13:10











  • I'm not sure if this is what you want to achieve, that's why I'm posting this as an comment: jsfiddle.net/edsLrjmu/1 (Example contains two possible solutions)

    – Jns
    Nov 14 '18 at 13:46













2












2








2








In vue, we have got directives v-if which needs to be attached to element.



Is there a way to use conditionals without attaching them to anything like the mustachejs way?



I am looping through an array of words and it is adding div's in every word which is annoying



Here is my template



<div v-for="(str, index) in reference" :key="index">
<div v-if="patternIncluded(str)">
<input type="text" v-model="remarks[index]">
</div>
<div v-else>
str
</div>
</div>


I would be nice if i could do it like this:|



if true
something goes here
else
other thing goes here
if









share|improve this question
















In vue, we have got directives v-if which needs to be attached to element.



Is there a way to use conditionals without attaching them to anything like the mustachejs way?



I am looping through an array of words and it is adding div's in every word which is annoying



Here is my template



<div v-for="(str, index) in reference" :key="index">
<div v-if="patternIncluded(str)">
<input type="text" v-model="remarks[index]">
</div>
<div v-else>
str
</div>
</div>


I would be nice if i could do it like this:|



if true
something goes here
else
other thing goes here
if






vue.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 13:10







Raaz

















asked Nov 14 '18 at 13:07









RaazRaaz

7541031




7541031












  • You'd probably use a ternary in an expression, but we probably need to see some code to help you

    – George Jempty
    Nov 14 '18 at 13:09











  • @GeorgeJempty i have updated my question

    – Raaz
    Nov 14 '18 at 13:10











  • I'm not sure if this is what you want to achieve, that's why I'm posting this as an comment: jsfiddle.net/edsLrjmu/1 (Example contains two possible solutions)

    – Jns
    Nov 14 '18 at 13:46

















  • You'd probably use a ternary in an expression, but we probably need to see some code to help you

    – George Jempty
    Nov 14 '18 at 13:09











  • @GeorgeJempty i have updated my question

    – Raaz
    Nov 14 '18 at 13:10











  • I'm not sure if this is what you want to achieve, that's why I'm posting this as an comment: jsfiddle.net/edsLrjmu/1 (Example contains two possible solutions)

    – Jns
    Nov 14 '18 at 13:46
















You'd probably use a ternary in an expression, but we probably need to see some code to help you

– George Jempty
Nov 14 '18 at 13:09





You'd probably use a ternary in an expression, but we probably need to see some code to help you

– George Jempty
Nov 14 '18 at 13:09













@GeorgeJempty i have updated my question

– Raaz
Nov 14 '18 at 13:10





@GeorgeJempty i have updated my question

– Raaz
Nov 14 '18 at 13:10













I'm not sure if this is what you want to achieve, that's why I'm posting this as an comment: jsfiddle.net/edsLrjmu/1 (Example contains two possible solutions)

– Jns
Nov 14 '18 at 13:46





I'm not sure if this is what you want to achieve, that's why I'm posting this as an comment: jsfiddle.net/edsLrjmu/1 (Example contains two possible solutions)

– Jns
Nov 14 '18 at 13:46












2 Answers
2






active

oldest

votes


















1














Here is your code but using the < template > tag



<template v-if="patternIncluded(str)">
<input type="text" v-model="remarks[index]">
</template>
<template v-else>
str
</template>


Here is a example from the official Vue documentation:



<template v-if="loginType === 'username'">
<label>Username</label>
<input placeholder="Enter your username">
</template>
<template v-else>
<label>Email</label>
<input placeholder="Enter your email address">
</template>


More informations: https://vuejs.org/v2/guide/conditional.html#Controlling-Reusable-Elements-with-key



You can also use a Method and use normal Javascript into the < script > at the bottom of your file



Hope this helped, cheers






share|improve this answer

























  • wrapping it inside a template too creates a div

    – Raaz
    Nov 15 '18 at 3:39











  • @Raaz you should replace the div with the v-if with the template: I have updated my answer

    – Niels Lucas
    Nov 15 '18 at 8:27



















0














you could use the 'template' like so:



<template v-if=patternIncluded(str)>
//code
</template

<template v-else>
//code
</template>


UPDATE so like this



<div-for="(str, index) in reference" :key="index">
<template v-if="patternIncluded(str)">
<input type="text" v-model="remarks[index]">
</template>
<template v-else>
str
</template>
</div>





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%2f53300964%2fhow-to-use-conditionals-without-attaching-it-to-an-element%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














    Here is your code but using the < template > tag



    <template v-if="patternIncluded(str)">
    <input type="text" v-model="remarks[index]">
    </template>
    <template v-else>
    str
    </template>


    Here is a example from the official Vue documentation:



    <template v-if="loginType === 'username'">
    <label>Username</label>
    <input placeholder="Enter your username">
    </template>
    <template v-else>
    <label>Email</label>
    <input placeholder="Enter your email address">
    </template>


    More informations: https://vuejs.org/v2/guide/conditional.html#Controlling-Reusable-Elements-with-key



    You can also use a Method and use normal Javascript into the < script > at the bottom of your file



    Hope this helped, cheers






    share|improve this answer

























    • wrapping it inside a template too creates a div

      – Raaz
      Nov 15 '18 at 3:39











    • @Raaz you should replace the div with the v-if with the template: I have updated my answer

      – Niels Lucas
      Nov 15 '18 at 8:27
















    1














    Here is your code but using the < template > tag



    <template v-if="patternIncluded(str)">
    <input type="text" v-model="remarks[index]">
    </template>
    <template v-else>
    str
    </template>


    Here is a example from the official Vue documentation:



    <template v-if="loginType === 'username'">
    <label>Username</label>
    <input placeholder="Enter your username">
    </template>
    <template v-else>
    <label>Email</label>
    <input placeholder="Enter your email address">
    </template>


    More informations: https://vuejs.org/v2/guide/conditional.html#Controlling-Reusable-Elements-with-key



    You can also use a Method and use normal Javascript into the < script > at the bottom of your file



    Hope this helped, cheers






    share|improve this answer

























    • wrapping it inside a template too creates a div

      – Raaz
      Nov 15 '18 at 3:39











    • @Raaz you should replace the div with the v-if with the template: I have updated my answer

      – Niels Lucas
      Nov 15 '18 at 8:27














    1












    1








    1







    Here is your code but using the < template > tag



    <template v-if="patternIncluded(str)">
    <input type="text" v-model="remarks[index]">
    </template>
    <template v-else>
    str
    </template>


    Here is a example from the official Vue documentation:



    <template v-if="loginType === 'username'">
    <label>Username</label>
    <input placeholder="Enter your username">
    </template>
    <template v-else>
    <label>Email</label>
    <input placeholder="Enter your email address">
    </template>


    More informations: https://vuejs.org/v2/guide/conditional.html#Controlling-Reusable-Elements-with-key



    You can also use a Method and use normal Javascript into the < script > at the bottom of your file



    Hope this helped, cheers






    share|improve this answer















    Here is your code but using the < template > tag



    <template v-if="patternIncluded(str)">
    <input type="text" v-model="remarks[index]">
    </template>
    <template v-else>
    str
    </template>


    Here is a example from the official Vue documentation:



    <template v-if="loginType === 'username'">
    <label>Username</label>
    <input placeholder="Enter your username">
    </template>
    <template v-else>
    <label>Email</label>
    <input placeholder="Enter your email address">
    </template>


    More informations: https://vuejs.org/v2/guide/conditional.html#Controlling-Reusable-Elements-with-key



    You can also use a Method and use normal Javascript into the < script > at the bottom of your file



    Hope this helped, cheers







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 15:44

























    answered Nov 14 '18 at 15:27









    CedCed

    424217




    424217












    • wrapping it inside a template too creates a div

      – Raaz
      Nov 15 '18 at 3:39











    • @Raaz you should replace the div with the v-if with the template: I have updated my answer

      – Niels Lucas
      Nov 15 '18 at 8:27


















    • wrapping it inside a template too creates a div

      – Raaz
      Nov 15 '18 at 3:39











    • @Raaz you should replace the div with the v-if with the template: I have updated my answer

      – Niels Lucas
      Nov 15 '18 at 8:27

















    wrapping it inside a template too creates a div

    – Raaz
    Nov 15 '18 at 3:39





    wrapping it inside a template too creates a div

    – Raaz
    Nov 15 '18 at 3:39













    @Raaz you should replace the div with the v-if with the template: I have updated my answer

    – Niels Lucas
    Nov 15 '18 at 8:27






    @Raaz you should replace the div with the v-if with the template: I have updated my answer

    – Niels Lucas
    Nov 15 '18 at 8:27














    0














    you could use the 'template' like so:



    <template v-if=patternIncluded(str)>
    //code
    </template

    <template v-else>
    //code
    </template>


    UPDATE so like this



    <div-for="(str, index) in reference" :key="index">
    <template v-if="patternIncluded(str)">
    <input type="text" v-model="remarks[index]">
    </template>
    <template v-else>
    str
    </template>
    </div>





    share|improve this answer





























      0














      you could use the 'template' like so:



      <template v-if=patternIncluded(str)>
      //code
      </template

      <template v-else>
      //code
      </template>


      UPDATE so like this



      <div-for="(str, index) in reference" :key="index">
      <template v-if="patternIncluded(str)">
      <input type="text" v-model="remarks[index]">
      </template>
      <template v-else>
      str
      </template>
      </div>





      share|improve this answer



























        0












        0








        0







        you could use the 'template' like so:



        <template v-if=patternIncluded(str)>
        //code
        </template

        <template v-else>
        //code
        </template>


        UPDATE so like this



        <div-for="(str, index) in reference" :key="index">
        <template v-if="patternIncluded(str)">
        <input type="text" v-model="remarks[index]">
        </template>
        <template v-else>
        str
        </template>
        </div>





        share|improve this answer















        you could use the 'template' like so:



        <template v-if=patternIncluded(str)>
        //code
        </template

        <template v-else>
        //code
        </template>


        UPDATE so like this



        <div-for="(str, index) in reference" :key="index">
        <template v-if="patternIncluded(str)">
        <input type="text" v-model="remarks[index]">
        </template>
        <template v-else>
        str
        </template>
        </div>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 8:29

























        answered Nov 14 '18 at 14:19









        Niels LucasNiels Lucas

        721113




        721113



























            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%2f53300964%2fhow-to-use-conditionals-without-attaching-it-to-an-element%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            Evgeni Malkin