Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default value inside ngSelect

I have a a ngSelect with some options in it.

<select data-ng-model="type" data-ng-change="option(type)">
   <option data-ng-repeat="type in languages" value="{{type.i18n}}">
        {{type.language}}
    </option>
</select>

And a Controller

angular.module('navigation', [])
.controller('NavCtrl',['$scope','$translate', function($scope,$translate){

    $scope.option = function(type){
        console.log(type) //this display the i18n value of languages
        $translate.use(type);
    }

    $scope.languages = [
        { language: "English", i18n: "en_EN"},
        { language: "Swedish", i18n : "se_SE" }
    ];
}])

I want the ngSelect to have a default option, in my case: "English". I've tried to set it to:

$scope.type = $scope.languages[0].language; // English
$scope.type = $scope.languages[0]; //The whole darn json object.

Help please?

like image 750
petur Avatar asked Feb 25 '14 07:02

petur


2 Answers

Try this way

<select  ng-model="selectedlanguage" ng-change="option(this.selectedlanguage)"  ng-options="i.language for i in languages">
            </select>

//Js code

angular.module('navigation', [])
.controller('NavCtrl',['$scope','$translate', function($scope,$translate){

    $scope.option = function(type){
        console.log(type) //this display the i18n value of languages
        $translate.use(type);
    }

    $scope.languages = [
        { language: "English", i18n: "en_EN"},
        { language: "Swedish", i18n : "se_SE" }
    ];

    **$scope.selectedlanguage = $scope.languages[0];**
}])
like image 158
Ramesh Rajendran Avatar answered Sep 27 '22 19:09

Ramesh Rajendran


how about using ngSelected? http://docs.angularjs.org/api/ng/directive/ngSelected

<option ng-selected="$index==0"></option>

or

<option ng-selected="type.language=='English'"></option>
like image 21
doodeec Avatar answered Sep 27 '22 20:09

doodeec