Angular 7 select tag default option value issue












0















First of all, hello everyone. Here is the issue: I have 2 select tag. The first select tag has 2 options tag: first option tag has a value used as a default value (kind of placeholder +> "select" or "choose" etc), and second option tag is a ngFor loop with some street names.



The second select tag has just one option tag with a ngFor loop displaying some city names.



Whenever i click on a city name, the street select tag display its street names inside a dropdown.



At the very beginning when the window is loaded, the default value option ("select") appears well by default but as soon as i choose a city option, the default value "select" of the street select tag, appears inside the dropdown list but the first index of the corresponding street list, appears as default.



It is really frustating i can't find the reason why.



 <select class="dropdown-select" formControlName="street-dd">
<option class="dropdown-select" disabled selected [ngValue]="null">Select</option>
<option class="dropdown-select" *ngFor="let street of retrievedStreets">{{street}}</option>
</select>

<select class="dropdown-select"(change)="onCitySelected($event)" formControlName="city">
<option class="dropdown-select" *ngFor="let city of retrievedCities" [value]="city['city']">{{city["city"]}}</option>
</select>


Here the TS file :



export class AddressComponent  {
public retrievedCities: Array<any> = ;
public retrievedStreets: Array<any> = ;

private retrieveCities(cities: any) {
this.retrievedCities = ;
if (cities && cities.length > 0) {
this.retrievedCities = cities;
}
}

public onCitySelected(city) {
this.retrievedStreets = null;
this.retrievedStreets = this.lookupStreets(city);
console.log('retrieved street', this.retrievedStreets);
this.retrievedStreets = this.retrievedStreets.sort();
}

private lookupStreets(city): Array<any> {
return find(this.retrievedCities, {'city': city.target.value})['streets'];
}
}


At the beginning there is no issue the default value is displayed as placeholder:



enter image description here



But then when i choose a value of the street select inside the dropdrown, and then click on another value of the city select, the first index of the street list appears as placeholder and "select" appears disabled inside the dropdown list :



enter image description here



nd when i click on the dropdown list of the Street i get:




  • Select //which is disabled

  • CH DU LAC-A-L'ORIGINAL // first index appears as default

  • RUE MAUDE

  • etc ...


How can i force the street select to put "select" as a default value placeholder each time i switch the city option ???



Thanks everyone










share|improve this question





























    0















    First of all, hello everyone. Here is the issue: I have 2 select tag. The first select tag has 2 options tag: first option tag has a value used as a default value (kind of placeholder +> "select" or "choose" etc), and second option tag is a ngFor loop with some street names.



    The second select tag has just one option tag with a ngFor loop displaying some city names.



    Whenever i click on a city name, the street select tag display its street names inside a dropdown.



    At the very beginning when the window is loaded, the default value option ("select") appears well by default but as soon as i choose a city option, the default value "select" of the street select tag, appears inside the dropdown list but the first index of the corresponding street list, appears as default.



    It is really frustating i can't find the reason why.



     <select class="dropdown-select" formControlName="street-dd">
    <option class="dropdown-select" disabled selected [ngValue]="null">Select</option>
    <option class="dropdown-select" *ngFor="let street of retrievedStreets">{{street}}</option>
    </select>

    <select class="dropdown-select"(change)="onCitySelected($event)" formControlName="city">
    <option class="dropdown-select" *ngFor="let city of retrievedCities" [value]="city['city']">{{city["city"]}}</option>
    </select>


    Here the TS file :



    export class AddressComponent  {
    public retrievedCities: Array<any> = ;
    public retrievedStreets: Array<any> = ;

    private retrieveCities(cities: any) {
    this.retrievedCities = ;
    if (cities && cities.length > 0) {
    this.retrievedCities = cities;
    }
    }

    public onCitySelected(city) {
    this.retrievedStreets = null;
    this.retrievedStreets = this.lookupStreets(city);
    console.log('retrieved street', this.retrievedStreets);
    this.retrievedStreets = this.retrievedStreets.sort();
    }

    private lookupStreets(city): Array<any> {
    return find(this.retrievedCities, {'city': city.target.value})['streets'];
    }
    }


    At the beginning there is no issue the default value is displayed as placeholder:



    enter image description here



    But then when i choose a value of the street select inside the dropdrown, and then click on another value of the city select, the first index of the street list appears as placeholder and "select" appears disabled inside the dropdown list :



    enter image description here



    nd when i click on the dropdown list of the Street i get:




    • Select //which is disabled

    • CH DU LAC-A-L'ORIGINAL // first index appears as default

    • RUE MAUDE

    • etc ...


    How can i force the street select to put "select" as a default value placeholder each time i switch the city option ???



    Thanks everyone










    share|improve this question



























      0












      0








      0








      First of all, hello everyone. Here is the issue: I have 2 select tag. The first select tag has 2 options tag: first option tag has a value used as a default value (kind of placeholder +> "select" or "choose" etc), and second option tag is a ngFor loop with some street names.



      The second select tag has just one option tag with a ngFor loop displaying some city names.



      Whenever i click on a city name, the street select tag display its street names inside a dropdown.



      At the very beginning when the window is loaded, the default value option ("select") appears well by default but as soon as i choose a city option, the default value "select" of the street select tag, appears inside the dropdown list but the first index of the corresponding street list, appears as default.



      It is really frustating i can't find the reason why.



       <select class="dropdown-select" formControlName="street-dd">
      <option class="dropdown-select" disabled selected [ngValue]="null">Select</option>
      <option class="dropdown-select" *ngFor="let street of retrievedStreets">{{street}}</option>
      </select>

      <select class="dropdown-select"(change)="onCitySelected($event)" formControlName="city">
      <option class="dropdown-select" *ngFor="let city of retrievedCities" [value]="city['city']">{{city["city"]}}</option>
      </select>


      Here the TS file :



      export class AddressComponent  {
      public retrievedCities: Array<any> = ;
      public retrievedStreets: Array<any> = ;

      private retrieveCities(cities: any) {
      this.retrievedCities = ;
      if (cities && cities.length > 0) {
      this.retrievedCities = cities;
      }
      }

      public onCitySelected(city) {
      this.retrievedStreets = null;
      this.retrievedStreets = this.lookupStreets(city);
      console.log('retrieved street', this.retrievedStreets);
      this.retrievedStreets = this.retrievedStreets.sort();
      }

      private lookupStreets(city): Array<any> {
      return find(this.retrievedCities, {'city': city.target.value})['streets'];
      }
      }


      At the beginning there is no issue the default value is displayed as placeholder:



      enter image description here



      But then when i choose a value of the street select inside the dropdrown, and then click on another value of the city select, the first index of the street list appears as placeholder and "select" appears disabled inside the dropdown list :



      enter image description here



      nd when i click on the dropdown list of the Street i get:




      • Select //which is disabled

      • CH DU LAC-A-L'ORIGINAL // first index appears as default

      • RUE MAUDE

      • etc ...


      How can i force the street select to put "select" as a default value placeholder each time i switch the city option ???



      Thanks everyone










      share|improve this question
















      First of all, hello everyone. Here is the issue: I have 2 select tag. The first select tag has 2 options tag: first option tag has a value used as a default value (kind of placeholder +> "select" or "choose" etc), and second option tag is a ngFor loop with some street names.



      The second select tag has just one option tag with a ngFor loop displaying some city names.



      Whenever i click on a city name, the street select tag display its street names inside a dropdown.



      At the very beginning when the window is loaded, the default value option ("select") appears well by default but as soon as i choose a city option, the default value "select" of the street select tag, appears inside the dropdown list but the first index of the corresponding street list, appears as default.



      It is really frustating i can't find the reason why.



       <select class="dropdown-select" formControlName="street-dd">
      <option class="dropdown-select" disabled selected [ngValue]="null">Select</option>
      <option class="dropdown-select" *ngFor="let street of retrievedStreets">{{street}}</option>
      </select>

      <select class="dropdown-select"(change)="onCitySelected($event)" formControlName="city">
      <option class="dropdown-select" *ngFor="let city of retrievedCities" [value]="city['city']">{{city["city"]}}</option>
      </select>


      Here the TS file :



      export class AddressComponent  {
      public retrievedCities: Array<any> = ;
      public retrievedStreets: Array<any> = ;

      private retrieveCities(cities: any) {
      this.retrievedCities = ;
      if (cities && cities.length > 0) {
      this.retrievedCities = cities;
      }
      }

      public onCitySelected(city) {
      this.retrievedStreets = null;
      this.retrievedStreets = this.lookupStreets(city);
      console.log('retrieved street', this.retrievedStreets);
      this.retrievedStreets = this.retrievedStreets.sort();
      }

      private lookupStreets(city): Array<any> {
      return find(this.retrievedCities, {'city': city.target.value})['streets'];
      }
      }


      At the beginning there is no issue the default value is displayed as placeholder:



      enter image description here



      But then when i choose a value of the street select inside the dropdrown, and then click on another value of the city select, the first index of the street list appears as placeholder and "select" appears disabled inside the dropdown list :



      enter image description here



      nd when i click on the dropdown list of the Street i get:




      • Select //which is disabled

      • CH DU LAC-A-L'ORIGINAL // first index appears as default

      • RUE MAUDE

      • etc ...


      How can i force the street select to put "select" as a default value placeholder each time i switch the city option ???



      Thanks everyone







      angular angular7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 30 '18 at 1:48









      Goncalo Peres

      1,4791619




      1,4791619










      asked Nov 25 '18 at 1:27









      Youssef HarkatiYoussef Harkati

      659




      659
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Actually there is a solution :) Just need to reset the formcontrol in case of reactive forms:



          this.yourFormGroup.get('nameOfYourFormControl').reset()



          Then the default value will be picked up again.



          Hope it helps someone someday






          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%2f53463918%2fangular-7-select-tag-default-option-value-issue%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Actually there is a solution :) Just need to reset the formcontrol in case of reactive forms:



            this.yourFormGroup.get('nameOfYourFormControl').reset()



            Then the default value will be picked up again.



            Hope it helps someone someday






            share|improve this answer




























              1














              Actually there is a solution :) Just need to reset the formcontrol in case of reactive forms:



              this.yourFormGroup.get('nameOfYourFormControl').reset()



              Then the default value will be picked up again.



              Hope it helps someone someday






              share|improve this answer


























                1












                1








                1







                Actually there is a solution :) Just need to reset the formcontrol in case of reactive forms:



                this.yourFormGroup.get('nameOfYourFormControl').reset()



                Then the default value will be picked up again.



                Hope it helps someone someday






                share|improve this answer













                Actually there is a solution :) Just need to reset the formcontrol in case of reactive forms:



                this.yourFormGroup.get('nameOfYourFormControl').reset()



                Then the default value will be picked up again.



                Hope it helps someone someday







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 12:03









                Youssef HarkatiYoussef Harkati

                659




                659
































                    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%2f53463918%2fangular-7-select-tag-default-option-value-issue%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

                    Wiesbaden

                    Marschland

                    Dieringhausen