angularjs – 角度选择不绑定到Ng模型

我的角度选择没有约束力.我可以告诉值是正确的,但是选择没有更新.如果值存在,为什么不绑定?

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

这是一个小提琴:

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

在执行Select时,您需要将id更改为字符串

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

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

接下来,您应该使用对象的属性而不仅仅是范围变量,这将确保正确的模型绑定

ng-model="mySelection.colorId"

对象可能是简单的东西

$scope.mySelection = {colorId : colorId };
相关文章
相关标签/搜索