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
    Nov 8 at 9:07










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










  • selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
    – rafbermudez
    Nov 8 at 9:20










  • Try trackBy. I haven't found anything about compareWith. Where did you get that from?
    – Günter Zöchbauer
    Nov 8 at 9:22










  • compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
    – rafbermudez
    Nov 8 at 9:45















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
    Nov 8 at 9:07










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










  • selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
    – rafbermudez
    Nov 8 at 9:20










  • Try trackBy. I haven't found anything about compareWith. Where did you get that from?
    – Günter Zöchbauer
    Nov 8 at 9:22










  • compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
    – rafbermudez
    Nov 8 at 9:45













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 Nov 8 at 10:22









Günter Zöchbauer

308k64911860




308k64911860










asked Nov 8 at 8:58









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
    Nov 8 at 9:07










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










  • selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
    – rafbermudez
    Nov 8 at 9:20










  • Try trackBy. I haven't found anything about compareWith. Where did you get that from?
    – Günter Zöchbauer
    Nov 8 at 9:22










  • compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
    – rafbermudez
    Nov 8 at 9:45


















  • [(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
    Nov 8 at 9:07










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










  • selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
    – rafbermudez
    Nov 8 at 9:20










  • Try trackBy. I haven't found anything about compareWith. Where did you get that from?
    – Günter Zöchbauer
    Nov 8 at 9:22










  • compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
    – rafbermudez
    Nov 8 at 9:45
















[(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
Nov 8 at 9:07




[(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
Nov 8 at 9:07












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




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












selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
– rafbermudez
Nov 8 at 9:20




selectedCountry is a class instance and countries list has a class instance with equals property values, but no option is selected.
– rafbermudez
Nov 8 at 9:20












Try trackBy. I haven't found anything about compareWith. Where did you get that from?
– Günter Zöchbauer
Nov 8 at 9:22




Try trackBy. I haven't found anything about compareWith. Where did you get that from?
– Günter Zöchbauer
Nov 8 at 9:22












compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
– rafbermudez
Nov 8 at 9:45




compareWith directive is not a core angular directive: github.com/angular/angular/pull/13349
– rafbermudez
Nov 8 at 9:45












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
    Nov 8 at 14:01










  • 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
    Nov 8 at 14:13


















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















    Required, but never shown

























    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
      Nov 8 at 14:01










    • 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
      Nov 8 at 14:13















    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
      Nov 8 at 14:01










    • 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
      Nov 8 at 14:13













    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 Nov 8 at 10:25









    Günter Zöchbauer

    308k64911860




    308k64911860












    • This is useful when the list of countries is changing but it does not solve my problem
      – rafbermudez
      Nov 8 at 14:01










    • 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
      Nov 8 at 14:13


















    • This is useful when the list of countries is changing but it does not solve my problem
      – rafbermudez
      Nov 8 at 14:01










    • 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
      Nov 8 at 14:13
















    This is useful when the list of countries is changing but it does not solve my problem
    – rafbermudez
    Nov 8 at 14:01




    This is useful when the list of countries is changing but it does not solve my problem
    – rafbermudez
    Nov 8 at 14:01












    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
    Nov 8 at 14:13




    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
    Nov 8 at 14:13












    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 Nov 8 at 14:13









        rafbermudez

        113




        113






























            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%2f53204349%2fngmodel-doesnt%25c2%25b4t-work-fine-with-select-tag-and-class-instances-in-angulardart-5%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini