Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 下拉列表中的选定值无效:无法设置选定值_Javascript_Angularjs - Fatal编程技术网

Javascript 下拉列表中的选定值无效:无法设置选定值

Javascript 下拉列表中的选定值无效:无法设置选定值,javascript,angularjs,Javascript,Angularjs,我有动态生成下拉列表的代码。 我正在使用ng repeat动态生成下拉列表,但未获得有效的选定值。 我已经更新了我的代码。 HTML <div ng-repeat="item in items"> <select ng-model="item.shareToOption" ng-options="c.value for c in shareToOptions"></select> </div> 更新 我不想对JSON对象做任何更改,

我有动态生成下拉列表的代码。 我正在使用ng repeat动态生成下拉列表,但未获得有效的选定值。
我已经更新了我的代码。
HTML

<div ng-repeat="item in items">
    <select ng-model="item.shareToOption" ng-options="c.value for c in shareToOptions"></select>
</div> 
更新 我不想对JSON对象做任何更改,如何在不更改JSON对象的情况下实现结果

使用下面的代码,我在模型中获得了正确的id,但值并没有得到更新

<select class="form-control input-sm" ng-model="item.blockName.id" name="blockName" 
              ng-options="choice.id as choice.value for choice in blockNameOptions">

</select>   

ngModel
通过参考而不是值进行比较

您需要引用
$scope.shareToOptions
中的对象:

$scope.shareToOptions = [
    {id:1,value:"AA1"},
    {id:2,value:"AA2"},
    {id:3,value:"AA3"},
    {id:4,value:"AA4"},
    {id:4,value:"AA5"},
    {id:4,value:"AA6"},
    {id:4,value:"AA7"}
];

$scope.items = [
    {
        "shareToOption" : $scope.shareToOptions[0]
    },
    {
        "shareToOption" : $scope.shareToOptions[1]
    },
    {
        "shareToOption" : $scope.shareToOptions[2]
    },
    {
        "shareToOption" : $scope.shareToOptions[3]
    }
];
演示

如果不想更改两个现有阵列,我建议在控制器初始化时重新映射$scope.items,以使用正确的引用:

var setReferences = function () {
    var items = $scope.items;
    for (var i = 0; i < items.length; i++) {
        var reference = $scope.shareToOptions.filter(function (option) {
            return option.id === items[i].shareToOption.id;
        })[0];

        items[i].shareToOption = reference;
    }
};

setReferences();
var setReferences=函数(){
var items=$scope.items;
对于(变量i=0;i

演示

:我不想对JSON对象进行任何更改,如何在不对JSON对象进行任何更改的情况下实现上述结果?两个数组是否需要与示例中的完全相同?您能否解释一下,我如何做到这一点?:现在我在模型中得到了更新的id,但我的值仍然旧。请参阅:再次更新我的答案。
$scope.shareToOptions = [
    {id:1,value:"AA1"},
    {id:2,value:"AA2"},
    {id:3,value:"AA3"},
    {id:4,value:"AA4"},
    {id:4,value:"AA5"},
    {id:4,value:"AA6"},
    {id:4,value:"AA7"}
];

$scope.items = [
    {
        "shareToOption" : $scope.shareToOptions[0]
    },
    {
        "shareToOption" : $scope.shareToOptions[1]
    },
    {
        "shareToOption" : $scope.shareToOptions[2]
    },
    {
        "shareToOption" : $scope.shareToOptions[3]
    }
];
var setReferences = function () {
    var items = $scope.items;
    for (var i = 0; i < items.length; i++) {
        var reference = $scope.shareToOptions.filter(function (option) {
            return option.id === items[i].shareToOption.id;
        })[0];

        items[i].shareToOption = reference;
    }
};

setReferences();