AngularJS - 级联从同一来源选择

时间:2014-09-19 07:32:36

标签: javascript angularjs

我需要类似的东西:http://jsfiddle.net/PR6FM/,但是有角度地完成。 问题是我通过ng-repeat创建选择,它看起来像这样:

    <div ng-repeat="newCar in myCars">
        <select ng-model="newCar.carId" ng-options="car.carId as car.name for car in cars | filter: {'carId': '!'+myCars[$index-1].carId}"></select>
    </div>
    <button type="button" ng-click="myCars.push({'carId':myCars.length+1, 'name': "someName"})">ADD</button>

重点是过滤/删除已添加的新添加的选择框汽车的来源。

我知道过滤器:

filter: {'carId': '!'+myCars[$index-1].carId}

不是要走的路,但它是一个很好的领导。

2 个答案:

答案 0 :(得分:1)

看看这个小提琴:http://jsfiddle.net/jarydcarolin/aLyzn30m/1/

$scope.carArray = function (index) {
   var cars =  ['Renault', 'Holden', 'Ford','Dodge'];
   return cars.filter(function (el) {
       return ($scope.resultArray.map(function(e){return e.car}).slice(0,index).indexOf(el) === -1)
   })
}

这里的概念是首先设置你真正想要的级联数量,它可能并且可能与你选择的实际数组选项无关。我将它们设置为空对象,我们通过ng-repeat得到了很好的绑定,这是我们用普通数组得到的。

然后使用函数返回给定级联中SELECT的索引的数组,使每个选项列表动态化。此解决方案使逻辑超出子范围,因此行为是一致的。

答案 1 :(得分:-1)

尝试使用

<div ng-repeat="newCar in myCars | -carId">