I am new to AngularJS. I searched a lot, but it does not solve my problem.
I am getting a blank option for the first time in select box.
Here is my HTML code
<div ng-app="MyApp1">
<div ng-controller="MyController">
<input type="text" ng-model="feed.name" placeholder="Name" />
<select ng-model="feed.config">
<option ng-repeat="template in configs">{{template.name}}</option>
</select>
</div>
</div>
JS
var MyApp=angular.module('MyApp1',[])
MyApp.controller('MyController', function($scope) {
$scope.feed = {};
//Configuration
$scope.configs = [
{'name': 'Config 1',
'value': 'config1'},
{'name': 'Config 2',
'value': 'config2'},
{'name': 'Config 3',
'value': 'config3'}
];
//Setting first option as selected in configuration select
$scope.feed.config = $scope.configs[0].value;
});
But it doesn't seem to work. How can I get this solved? Here is JSFiddle Demo
For reference : Why does angularjs include an empty option in select?
Change your code like this
Working JSFiddle Demo
UPDATE (Dec 31, 2015)
If You don't want to set a default value and want to remove blank option,
And in JS no need to initialize value.
$scope.feed.config = $scope.feed.configs[0].value;
While all the suggestions above are working, sometimes I need to have an empty selection (showing placeholder text) when initially enter my screen. So, to prevent select box from adding this empty selection at the beginning (or sometimes at the end) of my list I am using this trick:
HTML Code
Now you have defined this empty option, so select box is happy, but you keep it hidden.
If you just want to remove the blank space use ng-show and you are done! No need to initialize value in JS.