Defaulting an Angular2 mariocatch Apr 14 '16 at 20:12

  • Next step is to implement slightly modified version of ngModel, check this [link](http://victorsavkin.com/post/119943127151/angular-2-template-syntax). – kemsky Apr 14 '16 at 20:26
  • 2

    On beta 14 (see: Angula2-beta.14 Changlog), angular adds the support for Object in select tags:

    <select class="form-control" required [(ngModel)]="model.product">
      <option *ngFor="#prod of products" 
       [ngValue]="prod">
         {{prod.name}}
      </option>
    </select>
    

    instead of [value], you can use [ngValue] to bind an object to ngModel in select tag.

    You can see a example on Plunker with default value.

    The attribute selected isn't working, I'm trying to fix this on my project with some workaround!

    Hope that can be useful.

    Maike Mota
    • 180
    • 1
    • 8
    0

    I exactly had the same issue, we can just bind the ngModel to the value :

    <option *ngFor="#product of products" [value]="model.product">{{product}}</option>
    
    Negin
    • 2,332
    • 17
    • 22
    0

    You are approaching in right direction. One thing that needs to consider is that null value of model is being compared with empty string value of default option. So if you compare and set null value of your default display text then it'll work fine. I have set as above and it's working for me !

    <select class="form-control" 
            required
            [(ngModel)]="model.product"
            ngControl="product">
        <option value="model.product === null ? null : ''">
                Select a product
        </option>
        <option *ngFor="#product of products" 
                [value]="product">{{product}}</option>
    </select>
    
    Pranav Labhe
    • 1,943
    • 1
    • 19
    • 24