1

I have a simple array:

items = ['All','item1','item2','item3'];

I have been tried

<select ng-model="ctrl.itemSelected" ng-change="ctrl.searchItem()">
    <option ng-repeat="item in ctrl.items" value="{{ctrl.items.indexOf(item)}}">{{item}}</option>
</select>

However, my select is being rendered in the page as

<select ... >
    <option value='?' selected> </option>
    <option value='0'> All </option>
    <option value='1'> Item1 </option>
    <option value='2'> Item2 </option>
    <option value='3'> Item3 </option>
</select>

So I would like this were <option value='0'> All </option> my option selected at the beginning

What can I do?

3
  • declare your initialize value like these - data-ng-init="ctrl.itemSelected=0" or from controller (js) Commented Jul 26, 2016 at 8:46
  • I tried it, but it was not enough. the first option with value="?" appears yet. Commented Jul 26, 2016 at 9:35
  • 1
    see here (plnkr.co/edit/I2MhSIx0ClL3XMLBSDFj?p=preview) Commented Jul 26, 2016 at 10:02

5 Answers 5

3

Just do in your controller to initialize itemSelected model:

ctrl.itemSelected = items[0];
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it, but it's not work yet. The first option value="?" has been the initial element when I load the page.
@Kelyane It seems that's because of value="{{ctrl.items.indexOf(item)}}". Just remove it and try, should work properly.
It works, but I also need the value as a number of my index array. Do you have any suggestion for it?
@Kelyane You can get it in the controller if it's acceptable for you, ex. ctrl.items.indexOf(ctrl.itemSelected)
Perfect! Thank you.
1

try this

<select ng-init="funcName= options[0]" ng-model="ctrl.itemSelected" ng-change="ctrl.searchItem()">
    <option ng-repeat="item in ctrl.items" value="{{ctrl.items.indexOf(item)}}">{{item}}</option>
</select>

Comments

1

The issue is that the model which you've kept for your select needs to have an initial value. Since ctrl.itemSelected have no initial value, so your select option is set to a blank value at starting

Initialize your select's model to a value from options. Like this

$scope.ctrl.itemSelected = 0;

Comments

1

Use ng-selected in option

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

app.controller('MainCtrl', function($scope) {
   $scope.items =  ['All','item1','item2','item3'];
   $scope.itemSelected = $scope.items[2];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<body ng-app="mapp" ng-controller="MainCtrl">
{{itemSelected}}
  
<select ng-model="itemSelected">
    <option  ng-repeat="item in items" ng-selected="{{item == itemSelected}}" value="{{items.indexOf(item)}}">{{item}}</option>
</select>
  
</body>

2 Comments

I also tried it, but it didn't work. I mean the item with ng-selected=true is correct, but when I load the page the first element is the option with value="?", however when I click in another option, this first element no longer appears.
I update the code with your array, Where is the error getting for you ?
1

array used as

$scope.items = ['All','item1','item2','item3'];

use the the Initialize the modal  
 ctrl.itemSelected = $scope.items[0];


<select ng-model="itemSelected">
    <option  ng-repeat="item in items" ng-selected="{{item == itemSelected}}" value="{{items.indexOf(item)}}">{{item}}</option>
</select>

Comments

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.