0

While appending the option values to select tag, an empty option tag is also getting appended using Angular JS. I couldn't figure it out why it adds so. Here is my JS code

    // Create global countries array.
    $scope.countries = [];

    countryPromise.success(function(data, status, headers, config) {
        for(index in data) {
                console.log("ID:"+data[index].id+" Name:"+data[index].name);
            $scope.countries.push({
                id:data[index].id,
                name:data[index].name                   
            });
        }
        $scope.countrylist = $scope.countries;

My HTML

<select ng-model="country" ng-change="changeCountry()" ng-options="v.name for v in countrylist">

Output of console

ID:1 Name:India 

2 Answers 2

1

If your model (country) is not set to a country then you will not see a selected value initially which will show as blank row when you first open the drop down, but after selecting an item that should go away. This is of course based on the assumption that your array does not have a null value.

Added an example fiddle:

http://jsfiddle.net/65RkM/1/

<select ng-model="country" ng-options="c.name for c in countrylist"></select>
Sign up to request clarification or add additional context in comments.

3 Comments

Doh! Of course, add $scope.country = $scope.countrylist[0] after $scope.countrylist = $scope.countries; to select the first country in the list by default.
@sjdaws that logic would solve the issue, but that might not be the desired business logic to apply & I updated my answer with some code to illustrate
@Brocco, in your fiddle, still the first value is null.
0

Most likely a blank record is being returned from your database. The simplest solution would be to check index for blanks.

// Create global countries array.
$scope.countries = [];

countryPromise.success(function(data, status, headers, config) {
    for(index in data) {
        if (data[index].id) {
            $scope.countries.push({
                id:data[index].id,
                name:data[index].name                   
            });
        }
    }
    $scope.countrylist = $scope.countries;

5 Comments

No. There is no blank record. Should I change anything in the html code?
Try adding an if clause to skip blank records like above, see if that fixes it. I use the exact same method as you did originally and don't get blank records so it has to be something funky. Did you try console.log(data) to check if there were absolutely no blank records?
No. That doesn't fix. I'm pretty sure that there is only one record and I could see in console when I print it.
Can you edit your original post with the output of console.log(data)?
Yeah, @Brocco was onto it, I've commented on his correct answer.

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.