简体   繁体   English

AngularJS-级联选择同一来源

[英]AngularJS - cascading select from same source

I need something similar to this: http://jsfiddle.net/PR6FM/ , but done in angular way. 我需要类似的东西: http : //jsfiddle.net/PR6FM/ ,但是要以有角度的方式完成。 The problem is that Im creating the selects by ng-repeat, it looks like this: 问题是我通过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>

The point is to filter/remove from the source of new added selectbox cars that has been already selected. 关键是要从已选择的新添加的选择框汽车的源中过滤/删除。

I know that the filter: 我知道过滤器:

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

is not the way to go, but it is a good lead. 不是要走的路,但这是一个很好的线索。

Have a look at this fiddle: http://jsfiddle.net/jarydcarolin/aLyzn30m/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)
   })
}

The concept here is to first set up how many cascades you actually want, it's possible and likely that it's independent from your actual array of options to select from. 这里的概念是首先设置您实际想要的级联数量,它有可能且可能独立于您要选择的实际选项阵列。 I set them up as empty objects to we get nice binding through the ng-repeat which we wouldn't get with an ordinary array. 我将它们设置为空对象,以便通过ng-repeat获得良好的绑定,而这是普通数组无法实现的。

Then make each options list dynamic by using a function to return the array given the index of that SELECT within the cascade. 然后,使用给定级联中SELECT索引的函数,通过返回一个数组使每个选项列表动态化。 This solution keeps the logic out of the child scopes so behaviour is consistent. 此解决方案使逻辑保持在子作用域之外,因此行为保持一致。

尝试使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM