Search code examples
angularjsangular-ngmodelangular-servicesangularjs-ng-options

ngOption default selected option


How to have a default option in select box...I tried several options and do not get it

I tried creating a scope on my contralador with the value of the object that comes from json:

Like this:

$scope.productSelect = $scope.item[0];

this my code:

html:

<select id="variant" class="form-control variant-select"
        ng-model="productSelect"
        ng-options="product as product.formattedPrice+' - '+product.variantQualifierName for product in item[0] track by product.url">

JS:

(function (){

  'use strict';

  /**
   * @ngdoc function
   * @name variantApp.controller:AppCtrl
   * @description
   * # AppCtrl
   * Controller of the variantApp
   */

   var app = angular.module('variantApp.product.variantCtrl', []);

   app.controller('AppCtrl',
    [
      '$scope',
      'ProductVariant',
      '$log',
      function ($scope, ProductVariant, $log){

      $scope.item = [
        ProductVariant.getData()
      ];

     $scope.productSelect = $scope.item[0];

     $log.info($scope.productSelect);


   }]);

})(window, angular);

Solution

  • You have ng-options="product as product.formattedPrice+' - '+product.variantQualifierName for product in item[0] track by product.url" And $scope.productSelect = $scope.item[0];

    So either you're trying to select out of one option or you're initializing your productSelected to the same array.