4

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/

1
  • See my answer. You are still using '1', '2', '3' for ids in your object. You need to set $scope.colorId to '1', '2', or '3' to have it bind, or you can update each id to be number (i.e. 1, 2, 3) instead of string. Commented Dec 4, 2014 at 19:34

3 Answers 3

7

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 };
Sign up to request clarification or add additional context in comments.

Comments

2

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;
    }
}

Comments

0

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/

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.