Jeremy Wagner
Jeremy Wagner

Reputation: 515

AngularJS Select Not Binding To Ng-Model

My angular select isn't binding. I can tell the value is correct, but the select isn't updated. Why is not binding if the value is there?

<div ng-controller="MyController" ng-app>
    <select class="form-control" ng-model="colorId"ng-options="color.id as color.name for color in colorList">
        <option value="">--Select a Color--</option>
    </select>
<input type="button" value="submit" ng-click="Select()"></input>

function MyController($scope) {
    $scope.colorList = [{
        id: '1',
        name: 'red'
    }, {
        id: '2',
        name: 'blue'
    }, {
        id: '3',
        name: 'green'
    }];

    var colorId = 3;
    $scope.colorId = colorId;
    alert($scope.colorId);

    $scope.Select = function () {
        var colorId = 2;
        $scope.colorId = colorId;
    }
}

Here is a fiddle:

http://jsfiddle.net/ky5F4/23/

Upvotes: 4

Views: 15002

Answers (3)

Steve D
Steve D

Reputation: 51

Consider making your ng-model be an object, specifically one of the objects that are already in your $scope.colorList. If you do that you should be able to avoid the post-processing you're doing in the click handler.

So your select will look like this:

<select class="form-control" ng-model="selectedColor" 
        ng-options="color.name for color in colorList"></select>

One gotcha is that if you have an object in your controller that looks JUST LIKE your red object, like$scope.selectedColorObj = { id : '1', name:'red' } and set the select's ng-model to that option, it won't work. Angular will see that you're setting to the ng-model to an object that's not actually in your data source and add an extra option with value="?", so I use $filter in this case to grab the matching member of the array:

 $scope.colorId = '3';
 $scope.selectedColor = $filter('filter')( $scope.colorList,{ id: $scope.colorId})[0];

See http://jsfiddle.net/ky5F4/92/

Upvotes: 0

SoluableNonagon
SoluableNonagon

Reputation: 11752

you need to change the id to a string when doing Select

$scope.Select = function () {
    console.log('select fired');
    var colorId = 1;
    $scope.mySelection.colorId = colorId + "";
}

http://jsfiddle.net/bxkwfo0s/2/

next you should use a property of an object rather than just a scope variable, this will ensure proper model binding

ng-model="mySelection.colorId"

where the object could be something simple

$scope.mySelection = {colorId : colorId };

Upvotes: 7

Patrick
Patrick

Reputation: 6958

There are two errors with your code:

  1. You are using colorList as your model in ng-options, but you are calling it datasets in your controller.
  2. You use strings for the id, but set the $scope.colorId to a number.

Here is an updated fiddle changing ids to numbers and changing $scope.datasets to $scope.colorList

function MyController($scope) {
    $scope.colorList = [{
        id: 1,
        name: 'red'
    }, {
        id: 2,
        name: 'blue'
    }, {
        id: 3,
        name: 'green'
    }];

    var colorId = 3;
    $scope.colorId = colorId;
    alert($scope.colorId);

    $scope.Select = function () {
        var colorId = 2;
        $scope.colorId = colorId;
    }
}

Upvotes: 2

Related Questions