使用AngularJS中的ng选项隐藏另一个列表中的选定选项

时间:2015-02-13 03:40:18

标签: javascript arrays angularjs ng-options angularjs-ng-options

希望有人可以帮助应对这一挑战。

我有一系列机场用于两个单独的下拉列表。

<select data-ng-model="flightData.origin" data-ng-options="airport.code as airport.label for airport in flightData.airports" name="origin"></select>

<select data-ng-model="flightData.destination" data-ng-options="airport.code as airport.label for airport in flightData.airports" name="destination"></select>

这两个列表显然都有相同的选项。我想要做的是当您在原点选择"Sydney"时,从目标下拉列表中删除"Sydney"

作为参考,这是机场数据的样子。

this.airports = [{
    code: "TSV",
    label: "Townsville",
}, {
    code: "PER",
    label: "Perth",
}, {
    code: "BNE",
    label: "Brisbane",
}, {
    code: "MEL",
    label: "Melbourne",
}, {
    code: "KGI",
    label: "Kalgoorlie",
}, {
    code: "SYD",
    label: "Sydney",
},{
    code: "LAX",
    label: "Los Angeles",
}, {
    code: "JFK",
    label: "New York",
}, {
    code: "DEL",
    label: "New Dehli",
}];

据我所知,我很可能需要两个独立的阵列(1个用于原点,1个用于目的地),只是不确定如何接近它。

谢谢!

1 个答案:

答案 0 :(得分:0)

使用ng-change,您还需要两个列表。

<select data-ng-model="flightData.airports_origin" ng-change="changeOrigin()" data-ng-options="airport.code as airport.label for airport in flightData.airports" name="origin"></select>

<select data-ng-model="flightData.airports_destination"  data-ng-options="airport.code as airport.label for airport in flightData.airports" name="origin"></select>

在您的控制器中,注入filterFilter,然后在可用目的地机场列表中搜索所选机场并将其删除。

// in your controller
$scope.airports_origin      = angular.copy(this.airport); 
$scope.airports_destination = angular.copy(this.airport);
$scope.changeOrigin = function(){
    var selected_origin = flightData.origin;
    var destination_airport = filterFilter(this.airports_destination,{'code':selected_origin.code})[0];
    $scope.airports_destination.splice($scope.airports_destination.indexOf(destination_airport),1);

}