ngModel doesnt´t work fine with select tag and Class instances in Angulardart 5











up vote
0
down vote

favorite












In a select tag, I need two-way binding, but is not working.



<select [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>




  • selectedCountry is a Country instance: selectedCountry.id = 3


  • countries is a Country instance List: country1.id = 1, country3.id = 3, country3.id = 3


selectedCountry and country3 have equal values but they aren't the same instance. Then country3 option is not selected.



I have overwritten the equality (==) operator and hashCode for the Country class, but it still does not work.



What's the problem? Is there something similar to the angular compareWith directive?



<select [compareWith]="equals"  [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>




equals(o1: Country, o2: Country) {
return o1.id === o2.id;
}









share|improve this question
























  • [(ngModel)]="selectedCountries" sounds wrong. There can only be one value be selected. What type is selectedCountries and what value does it contain? What does "doesnt´t work fine" mean exactly?
    – Günter Zöchbauer
    yesterday










  • Instead of implementing operator== (which is usually a good idea anyway) you can also use webdev.dartlang.org/angular/guide/…
    – Günter Zöchbauer
    yesterday










  • selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
    – rafbermudez
    yesterday










  • Try trackBy. I haven't found anything about compareWith. Where did you get that from?
    – Günter Zöchbauer
    yesterday










  • compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
    – rafbermudez
    yesterday















up vote
0
down vote

favorite












In a select tag, I need two-way binding, but is not working.



<select [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>




  • selectedCountry is a Country instance: selectedCountry.id = 3


  • countries is a Country instance List: country1.id = 1, country3.id = 3, country3.id = 3


selectedCountry and country3 have equal values but they aren't the same instance. Then country3 option is not selected.



I have overwritten the equality (==) operator and hashCode for the Country class, but it still does not work.



What's the problem? Is there something similar to the angular compareWith directive?



<select [compareWith]="equals"  [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>




equals(o1: Country, o2: Country) {
return o1.id === o2.id;
}









share|improve this question
























  • [(ngModel)]="selectedCountries" sounds wrong. There can only be one value be selected. What type is selectedCountries and what value does it contain? What does "doesnt´t work fine" mean exactly?
    – Günter Zöchbauer
    yesterday










  • Instead of implementing operator== (which is usually a good idea anyway) you can also use webdev.dartlang.org/angular/guide/…
    – Günter Zöchbauer
    yesterday










  • selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
    – rafbermudez
    yesterday










  • Try trackBy. I haven't found anything about compareWith. Where did you get that from?
    – Günter Zöchbauer
    yesterday










  • compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
    – rafbermudez
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In a select tag, I need two-way binding, but is not working.



<select [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>




  • selectedCountry is a Country instance: selectedCountry.id = 3


  • countries is a Country instance List: country1.id = 1, country3.id = 3, country3.id = 3


selectedCountry and country3 have equal values but they aren't the same instance. Then country3 option is not selected.



I have overwritten the equality (==) operator and hashCode for the Country class, but it still does not work.



What's the problem? Is there something similar to the angular compareWith directive?



<select [compareWith]="equals"  [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>




equals(o1: Country, o2: Country) {
return o1.id === o2.id;
}









share|improve this question















In a select tag, I need two-way binding, but is not working.



<select [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>




  • selectedCountry is a Country instance: selectedCountry.id = 3


  • countries is a Country instance List: country1.id = 1, country3.id = 3, country3.id = 3


selectedCountry and country3 have equal values but they aren't the same instance. Then country3 option is not selected.



I have overwritten the equality (==) operator and hashCode for the Country class, but it still does not work.



What's the problem? Is there something similar to the angular compareWith directive?



<select [compareWith]="equals"  [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>




equals(o1: Country, o2: Country) {
return o1.id === o2.id;
}






dart angular-dart






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Günter Zöchbauer

302k59888837




302k59888837










asked yesterday









rafbermudez

113




113












  • [(ngModel)]="selectedCountries" sounds wrong. There can only be one value be selected. What type is selectedCountries and what value does it contain? What does "doesnt´t work fine" mean exactly?
    – Günter Zöchbauer
    yesterday










  • Instead of implementing operator== (which is usually a good idea anyway) you can also use webdev.dartlang.org/angular/guide/…
    – Günter Zöchbauer
    yesterday










  • selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
    – rafbermudez
    yesterday










  • Try trackBy. I haven't found anything about compareWith. Where did you get that from?
    – Günter Zöchbauer
    yesterday










  • compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
    – rafbermudez
    yesterday


















  • [(ngModel)]="selectedCountries" sounds wrong. There can only be one value be selected. What type is selectedCountries and what value does it contain? What does "doesnt´t work fine" mean exactly?
    – Günter Zöchbauer
    yesterday










  • Instead of implementing operator== (which is usually a good idea anyway) you can also use webdev.dartlang.org/angular/guide/…
    – Günter Zöchbauer
    yesterday










  • selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
    – rafbermudez
    yesterday










  • Try trackBy. I haven't found anything about compareWith. Where did you get that from?
    – Günter Zöchbauer
    yesterday










  • compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
    – rafbermudez
    yesterday
















[(ngModel)]="selectedCountries" sounds wrong. There can only be one value be selected. What type is selectedCountries and what value does it contain? What does "doesnt´t work fine" mean exactly?
– Günter Zöchbauer
yesterday




[(ngModel)]="selectedCountries" sounds wrong. There can only be one value be selected. What type is selectedCountries and what value does it contain? What does "doesnt´t work fine" mean exactly?
– Günter Zöchbauer
yesterday












Instead of implementing operator== (which is usually a good idea anyway) you can also use webdev.dartlang.org/angular/guide/…
– Günter Zöchbauer
yesterday




Instead of implementing operator== (which is usually a good idea anyway) you can also use webdev.dartlang.org/angular/guide/…
– Günter Zöchbauer
yesterday












selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
– rafbermudez
yesterday




selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
– rafbermudez
yesterday












Try trackBy. I haven't found anything about compareWith. Where did you get that from?
– Günter Zöchbauer
yesterday




Try trackBy. I haven't found anything about compareWith. Where did you get that from?
– Günter Zöchbauer
yesterday












compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
– rafbermudez
yesterday




compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
– rafbermudez
yesterday












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Is there something similar to the angular compareWith directive?



You can use trackBy https://webdev.dartlang.org/angular/guide/template-syntax#ngfor-with-trackby



Object trackByCountryId(_, dynamic o) => o is Country ? o.id : o;




<select *ngFor="let hero of heroes; trackBy: trackByCountryId" [(ngModel)]="selectedCountry">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>





share|improve this answer





















  • This is useful when the list of countries is changing but it does not solve my problem
    – rafbermudez
    22 hours ago










  • No idea what you are talking about. That exactly to solve the issue your question is about. I don't know what you mean with "when the list of countries is changing". I don't see that related.
    – Günter Zöchbauer
    22 hours ago


















up vote
0
down vote













I found the problem:



The use of the identical() function to compare values in select_control_value_accessor of the select ngModel instead ==.



I opened a github issue:
https://github.com/dart-lang/angular/issues/1659



Meanwhile, I'm not using two-way binding complex object:



<select [(ngModel)]="selectedCountryId">
<option *ngFor="let country of countries" [ngValue]="country.id">
{{country.name}}
</option>
</select>





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',
    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%2f53204349%2fngmodel-doesnt%25c2%25b4t-work-fine-with-select-tag-and-class-instances-in-angulardart-5%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Is there something similar to the angular compareWith directive?



    You can use trackBy https://webdev.dartlang.org/angular/guide/template-syntax#ngfor-with-trackby



    Object trackByCountryId(_, dynamic o) => o is Country ? o.id : o;




    <select *ngFor="let hero of heroes; trackBy: trackByCountryId" [(ngModel)]="selectedCountry">
    <option *ngFor="let country of countries" [ngValue]="country">
    {{country.name}}
    </option>
    </select>





    share|improve this answer





















    • This is useful when the list of countries is changing but it does not solve my problem
      – rafbermudez
      22 hours ago










    • No idea what you are talking about. That exactly to solve the issue your question is about. I don't know what you mean with "when the list of countries is changing". I don't see that related.
      – Günter Zöchbauer
      22 hours ago















    up vote
    0
    down vote













    Is there something similar to the angular compareWith directive?



    You can use trackBy https://webdev.dartlang.org/angular/guide/template-syntax#ngfor-with-trackby



    Object trackByCountryId(_, dynamic o) => o is Country ? o.id : o;




    <select *ngFor="let hero of heroes; trackBy: trackByCountryId" [(ngModel)]="selectedCountry">
    <option *ngFor="let country of countries" [ngValue]="country">
    {{country.name}}
    </option>
    </select>





    share|improve this answer





















    • This is useful when the list of countries is changing but it does not solve my problem
      – rafbermudez
      22 hours ago










    • No idea what you are talking about. That exactly to solve the issue your question is about. I don't know what you mean with "when the list of countries is changing". I don't see that related.
      – Günter Zöchbauer
      22 hours ago













    up vote
    0
    down vote










    up vote
    0
    down vote









    Is there something similar to the angular compareWith directive?



    You can use trackBy https://webdev.dartlang.org/angular/guide/template-syntax#ngfor-with-trackby



    Object trackByCountryId(_, dynamic o) => o is Country ? o.id : o;




    <select *ngFor="let hero of heroes; trackBy: trackByCountryId" [(ngModel)]="selectedCountry">
    <option *ngFor="let country of countries" [ngValue]="country">
    {{country.name}}
    </option>
    </select>





    share|improve this answer












    Is there something similar to the angular compareWith directive?



    You can use trackBy https://webdev.dartlang.org/angular/guide/template-syntax#ngfor-with-trackby



    Object trackByCountryId(_, dynamic o) => o is Country ? o.id : o;




    <select *ngFor="let hero of heroes; trackBy: trackByCountryId" [(ngModel)]="selectedCountry">
    <option *ngFor="let country of countries" [ngValue]="country">
    {{country.name}}
    </option>
    </select>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    Günter Zöchbauer

    302k59888837




    302k59888837












    • This is useful when the list of countries is changing but it does not solve my problem
      – rafbermudez
      22 hours ago










    • No idea what you are talking about. That exactly to solve the issue your question is about. I don't know what you mean with "when the list of countries is changing". I don't see that related.
      – Günter Zöchbauer
      22 hours ago


















    • This is useful when the list of countries is changing but it does not solve my problem
      – rafbermudez
      22 hours ago










    • No idea what you are talking about. That exactly to solve the issue your question is about. I don't know what you mean with "when the list of countries is changing". I don't see that related.
      – Günter Zöchbauer
      22 hours ago
















    This is useful when the list of countries is changing but it does not solve my problem
    – rafbermudez
    22 hours ago




    This is useful when the list of countries is changing but it does not solve my problem
    – rafbermudez
    22 hours ago












    No idea what you are talking about. That exactly to solve the issue your question is about. I don't know what you mean with "when the list of countries is changing". I don't see that related.
    – Günter Zöchbauer
    22 hours ago




    No idea what you are talking about. That exactly to solve the issue your question is about. I don't know what you mean with "when the list of countries is changing". I don't see that related.
    – Günter Zöchbauer
    22 hours ago












    up vote
    0
    down vote













    I found the problem:



    The use of the identical() function to compare values in select_control_value_accessor of the select ngModel instead ==.



    I opened a github issue:
    https://github.com/dart-lang/angular/issues/1659



    Meanwhile, I'm not using two-way binding complex object:



    <select [(ngModel)]="selectedCountryId">
    <option *ngFor="let country of countries" [ngValue]="country.id">
    {{country.name}}
    </option>
    </select>





    share|improve this answer

























      up vote
      0
      down vote













      I found the problem:



      The use of the identical() function to compare values in select_control_value_accessor of the select ngModel instead ==.



      I opened a github issue:
      https://github.com/dart-lang/angular/issues/1659



      Meanwhile, I'm not using two-way binding complex object:



      <select [(ngModel)]="selectedCountryId">
      <option *ngFor="let country of countries" [ngValue]="country.id">
      {{country.name}}
      </option>
      </select>





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I found the problem:



        The use of the identical() function to compare values in select_control_value_accessor of the select ngModel instead ==.



        I opened a github issue:
        https://github.com/dart-lang/angular/issues/1659



        Meanwhile, I'm not using two-way binding complex object:



        <select [(ngModel)]="selectedCountryId">
        <option *ngFor="let country of countries" [ngValue]="country.id">
        {{country.name}}
        </option>
        </select>





        share|improve this answer












        I found the problem:



        The use of the identical() function to compare values in select_control_value_accessor of the select ngModel instead ==.



        I opened a github issue:
        https://github.com/dart-lang/angular/issues/1659



        Meanwhile, I'm not using two-way binding complex object:



        <select [(ngModel)]="selectedCountryId">
        <option *ngFor="let country of countries" [ngValue]="country.id">
        {{country.name}}
        </option>
        </select>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 22 hours ago









        rafbermudez

        113




        113






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204349%2fngmodel-doesnt%25c2%25b4t-work-fine-with-select-tag-and-class-instances-in-angulardart-5%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)