0

Question

How do I add placeholder text to blank option created by angular ng-options.I would like the blank option of my drop downs to say "please select a size"

Caveat: * to note: it's a extra weird cuz I have the size collection being split(',') because it's not an array*

Live Code Fiddle

HTML:

<form ng-app="app" ng-controller="Ctrl">
    <div class="color-pick" ng-repeat="(key, val) in productData.colors_and_sizes.data">
        <input type="radio" name="colors" ng-model="$parent.myColor" ng-value="key" />
        <img ng-click="$parent.myColor = key" ng-src="{{val.swatch_image}}" alt="">{{key}} 
        <div class="size-pick" ng-show="$parent.myColor==key">
            <select ng-model="$parent.mySize" ng-options="size for size in val.sizes.split(',')"></select>
        </div>
    </div>
    myColor: {{myColor}}<br/>
    mySize: {{mySize}}
</form>

JS:

var app = angular.module("app", []);

app.controller('Ctrl', function ($scope, $filter, $http) {
 $scope.productData = {
  "colors_and_sizes": {
    "data": {
      "Black": {
        "swatch_image": 'http://lorempixel.com/50/50',
        "sizes": "X Small, Small, Medium, Large, Xlarge, XX Large"
      },
      "Blue": {
        "swatch_image": 'http://lorempixel.com/50/50',
        "sizes": "X Small, Small, Medium, Large, Xlarge, XX Large"
      }
    }
  }
};

});
4

1 Answer 1

1

Please see here http://jsfiddle.net/jg42xsop/ some people saying that we should use ng-init instead using $parent

<form ng-app="app" ng-controller="Ctrl">
    <div class="color-pick" ng-repeat="(key, val) in productData.colors_and_sizes.data" ng-init="item = this">
        <input type="radio" name="colors" ng-model="item.myColor" ng-value="key" />

        <img ng-click="item.myColor = key" ng-src="{{val.swatch_image}}" alt="">
            <p>{{key}} </p>

            <div class="size-pick"  ng-show="item.myColor==key">
            <select ng-model="item.mySize" ng-options="size for size in val.sizes.split(',')">
                 <option value="">Please choose a size</option>
            </select>

            </div>

            <p>  myColor: {{myColor}}</p>
            <p> mySize: {{mySize}} </p>
            <hr/>
        </div>
</form>
1
  • I feel stupid for not trying that lol. Thanks for the advice. Aug 11, 2014 at 23:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.