Angular ng-option索引问题

时间:2017-05-22 17:44:24

标签: angularjs json angularjs-ng-repeat

我正在使用Angular版本1中的ng-options。问题如下:

我有一个json文件,我读到这个文件来填充下拉选项:

$http({method: 'GET', url: 'collection_countries.json'})
        .then(function successCallback(response) {            
            $scope.collection_countries = response.data;
            $scope.collection_country=$scope.collection_countries[0]; // pre-seelct       
        }
 );

然后我按如下方式生成下拉列表:

<select class="form-control" name="deliver_to" ng-model="destination_country" ng-options="item as item.country for item in countries track by item.id" required>
     <option value="" disdabled>Select Country</option>
</select>

这点工作正常,但是我正在尝试从json中读取所选值和相应的数据,如下所示:

$scope.countries[ $scope.destination_country['country']['id'] ];

正如您所看到的,$ scope.destination_country [&#39; country&#39;] [&#39; id&#39;]的值是任意的ID,与json对象的索引不对应

我认为有两种选择:

  1. 在ng-options指令中,我跟踪$ index(或类似我知道$ index是针对ng-repeat而非ng-options)

  2. 如何通过country_id

  3. 引用json对象

    我创建了这个plunkr来演示这个问题。

    https://plnkr.co/edit/19H8A12VnVpdlHQe5vM7?p=preview

    感谢。

0 个答案:

没有答案