Angular5-Pass value of index to component












0















How should I pass the value of index of the option selected to the component.



<select (click)= "onpressed(i)" placeholder="select a value" class="custom-select"  >
<option *ngFor="let data of data3; let i = index;" value = {{data.dash_name}} >
{{data.dash_name}}
</option>
</select>









share|improve this question




















  • 1





    Post a clear and complete question, and post the relevant code in the question itself, as properly formatted text. Not as a link to an image.

    – JB Nizet
    Nov 14 '18 at 16:39











  • I have edited my question. @JBNizet

    – user10648256
    Nov 14 '18 at 16:58
















0















How should I pass the value of index of the option selected to the component.



<select (click)= "onpressed(i)" placeholder="select a value" class="custom-select"  >
<option *ngFor="let data of data3; let i = index;" value = {{data.dash_name}} >
{{data.dash_name}}
</option>
</select>









share|improve this question




















  • 1





    Post a clear and complete question, and post the relevant code in the question itself, as properly formatted text. Not as a link to an image.

    – JB Nizet
    Nov 14 '18 at 16:39











  • I have edited my question. @JBNizet

    – user10648256
    Nov 14 '18 at 16:58














0












0








0








How should I pass the value of index of the option selected to the component.



<select (click)= "onpressed(i)" placeholder="select a value" class="custom-select"  >
<option *ngFor="let data of data3; let i = index;" value = {{data.dash_name}} >
{{data.dash_name}}
</option>
</select>









share|improve this question
















How should I pass the value of index of the option selected to the component.



<select (click)= "onpressed(i)" placeholder="select a value" class="custom-select"  >
<option *ngFor="let data of data3; let i = index;" value = {{data.dash_name}} >
{{data.dash_name}}
</option>
</select>






angular






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 16:57







user10648256

















asked Nov 14 '18 at 16:33









user10648256user10648256

536




536








  • 1





    Post a clear and complete question, and post the relevant code in the question itself, as properly formatted text. Not as a link to an image.

    – JB Nizet
    Nov 14 '18 at 16:39











  • I have edited my question. @JBNizet

    – user10648256
    Nov 14 '18 at 16:58














  • 1





    Post a clear and complete question, and post the relevant code in the question itself, as properly formatted text. Not as a link to an image.

    – JB Nizet
    Nov 14 '18 at 16:39











  • I have edited my question. @JBNizet

    – user10648256
    Nov 14 '18 at 16:58








1




1





Post a clear and complete question, and post the relevant code in the question itself, as properly formatted text. Not as a link to an image.

– JB Nizet
Nov 14 '18 at 16:39





Post a clear and complete question, and post the relevant code in the question itself, as properly formatted text. Not as a link to an image.

– JB Nizet
Nov 14 '18 at 16:39













I have edited my question. @JBNizet

– user10648256
Nov 14 '18 at 16:58





I have edited my question. @JBNizet

– user10648256
Nov 14 '18 at 16:58












2 Answers
2






active

oldest

votes


















0














Use like this.



<select (change)="onChange($event.target.selectedIndex)">
<option *ngFor="let item of items; let i=index;" [value]="item.value">{{item.value}}</option>




Component:



onChange(index){
console.log(index);
}





share|improve this answer


























  • The value of index obtained in console is undefined.

    – user10648256
    Nov 14 '18 at 17:17











  • Updated the code. check now

    – Suresh Kumar Ariya
    Nov 14 '18 at 19:06



















0














Live Example Relevant code in in files modalTest.component.ts and app.component.html



Try this:



  <select class="form-control" [(ngModel)]="index">
<option *ngFor="let razon of razones; let i = index" [ngValue]="i">
{{razon.descRazon}}
</option>
</select>


In the component class



export class ModalTest  {

razones = [{
descRazon:'testing Uno'
},{
descRazon:'testing Dos'
}]

index = 0;

constructor(){}

}





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%2f53304837%2fangular5-pass-value-of-index-to-component%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









    0














    Use like this.



    <select (change)="onChange($event.target.selectedIndex)">
    <option *ngFor="let item of items; let i=index;" [value]="item.value">{{item.value}}</option>




    Component:



    onChange(index){
    console.log(index);
    }





    share|improve this answer


























    • The value of index obtained in console is undefined.

      – user10648256
      Nov 14 '18 at 17:17











    • Updated the code. check now

      – Suresh Kumar Ariya
      Nov 14 '18 at 19:06
















    0














    Use like this.



    <select (change)="onChange($event.target.selectedIndex)">
    <option *ngFor="let item of items; let i=index;" [value]="item.value">{{item.value}}</option>




    Component:



    onChange(index){
    console.log(index);
    }





    share|improve this answer


























    • The value of index obtained in console is undefined.

      – user10648256
      Nov 14 '18 at 17:17











    • Updated the code. check now

      – Suresh Kumar Ariya
      Nov 14 '18 at 19:06














    0












    0








    0







    Use like this.



    <select (change)="onChange($event.target.selectedIndex)">
    <option *ngFor="let item of items; let i=index;" [value]="item.value">{{item.value}}</option>




    Component:



    onChange(index){
    console.log(index);
    }





    share|improve this answer















    Use like this.



    <select (change)="onChange($event.target.selectedIndex)">
    <option *ngFor="let item of items; let i=index;" [value]="item.value">{{item.value}}</option>




    Component:



    onChange(index){
    console.log(index);
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 19:07

























    answered Nov 14 '18 at 16:41









    Suresh Kumar AriyaSuresh Kumar Ariya

    4,6611215




    4,6611215













    • The value of index obtained in console is undefined.

      – user10648256
      Nov 14 '18 at 17:17











    • Updated the code. check now

      – Suresh Kumar Ariya
      Nov 14 '18 at 19:06



















    • The value of index obtained in console is undefined.

      – user10648256
      Nov 14 '18 at 17:17











    • Updated the code. check now

      – Suresh Kumar Ariya
      Nov 14 '18 at 19:06

















    The value of index obtained in console is undefined.

    – user10648256
    Nov 14 '18 at 17:17





    The value of index obtained in console is undefined.

    – user10648256
    Nov 14 '18 at 17:17













    Updated the code. check now

    – Suresh Kumar Ariya
    Nov 14 '18 at 19:06





    Updated the code. check now

    – Suresh Kumar Ariya
    Nov 14 '18 at 19:06













    0














    Live Example Relevant code in in files modalTest.component.ts and app.component.html



    Try this:



      <select class="form-control" [(ngModel)]="index">
    <option *ngFor="let razon of razones; let i = index" [ngValue]="i">
    {{razon.descRazon}}
    </option>
    </select>


    In the component class



    export class ModalTest  {

    razones = [{
    descRazon:'testing Uno'
    },{
    descRazon:'testing Dos'
    }]

    index = 0;

    constructor(){}

    }





    share|improve this answer




























      0














      Live Example Relevant code in in files modalTest.component.ts and app.component.html



      Try this:



        <select class="form-control" [(ngModel)]="index">
      <option *ngFor="let razon of razones; let i = index" [ngValue]="i">
      {{razon.descRazon}}
      </option>
      </select>


      In the component class



      export class ModalTest  {

      razones = [{
      descRazon:'testing Uno'
      },{
      descRazon:'testing Dos'
      }]

      index = 0;

      constructor(){}

      }





      share|improve this answer


























        0












        0








        0







        Live Example Relevant code in in files modalTest.component.ts and app.component.html



        Try this:



          <select class="form-control" [(ngModel)]="index">
        <option *ngFor="let razon of razones; let i = index" [ngValue]="i">
        {{razon.descRazon}}
        </option>
        </select>


        In the component class



        export class ModalTest  {

        razones = [{
        descRazon:'testing Uno'
        },{
        descRazon:'testing Dos'
        }]

        index = 0;

        constructor(){}

        }





        share|improve this answer













        Live Example Relevant code in in files modalTest.component.ts and app.component.html



        Try this:



          <select class="form-control" [(ngModel)]="index">
        <option *ngFor="let razon of razones; let i = index" [ngValue]="i">
        {{razon.descRazon}}
        </option>
        </select>


        In the component class



        export class ModalTest  {

        razones = [{
        descRazon:'testing Uno'
        },{
        descRazon:'testing Dos'
        }]

        index = 0;

        constructor(){}

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 16:55









        Gabriel LopezGabriel Lopez

        28217




        28217






























            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%2f53304837%2fangular5-pass-value-of-index-to-component%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

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues