1

There is object:

   $scope.type = {
        1 : 'Inside',
        2 : 'Outside'
    };

HTML ng-options:

 <select ng-options="key as value for (key , value) in type" ng-model="selectedType"></select>

I tried:

$scope.selectedType = (UserGlobalService.type == 1) ? $scope.records[1] : $scope.records[2];

2 Answers 2

2

Hi this would solve your problem

Index.html

<html lang="en" ng-app='myApp'>
    <head>

        <title>My AngularJS App</title>

        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>
        <!-- Modules -->
        <script src="app.js"></script>
    </head>
    <body ng-controller ='MainController'>
        <div>

            <select class="form-control" >
            <!-- <option ng-value= "{{item}}" >{{item}}</option> -->
            <option ng-repeat="option in type" value="{{option.value}}" ng-selected="type.value == option.value">{{option.value}}
            </option>

        </select>               
        </div>

    </body>

And your JS should be like this.

var myApp = angular.module('myApp', []);

myApp.controller('MainController', ['$scope',
    function($scope) {
        $scope.type = [{
               "id" : 1,
               "value" : "Inside"
             },{
                "id" : 2,
               "value" : "Outside"
            }];        
    }
])
Sign up to request clarification or add additional context in comments.

3 Comments

I need select first element in select, not disabled
I need select first element from $scope.type
Hello @Hamed I have updated the code as per requirement.
1

I Edited my answer a bit

This is how I would do it:

$scope.types = [
   { id: 1, name: 'Inside' },
   { id: 2, name: 'Outside' }];

$scope.selectedType = $scope.type[0];

HTML:

<select ng-options="t.name for t in types" ng-model="selectedType"></select>

Here's Plunker: http://plnkr.co/edit/uyP5EeOUTQb7n1ymsFuB

4 Comments

This does not work for me. select list is empty. I get still empty select
Also instead $scope.selectedType = type[0]; must be $scope.selectedType = $scope.type[0];
When I select element from list I need change $scope.selectedType on selected index
I changed my answer and added a Plunker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.