AngularJS过滤多个选择输入
我真的很惊讶没有任何文档或关于AngularJS的这种更高级的过滤机制的问题 – 它对于任何UI都是必不可少的.
我需要过滤掉包含当前项目的选择选项.问题是当我们遍历进入过滤器的项目时,如何拼出每个选项,并为要过滤的列表中的每个选择返回正确的结果. 例如,如果我们有三个地区 – 加拿大,英国,美国 – 选择输入应如下所示: Canada [select] [option0] United Kingdom [option1] United States United Kingdom [select] [option0] Canada [option1] United States United States [select] [option0] Canada [option1] United Kingdom .. 等等… HTML: <div ng-repeat="region in data.regions"> <h2> {{region.name}} </h2> <input ui-select2="version2" type="hidden" name="keywordsLocal-{{$index}}" class="region-keywords input-xlarge" data-ng-model="data.regions[$index].keywords" required-multiple /> <select ui-select2 id="copy-{{$index}}" ng-show="region.length > 1" class="input-xlarge" ng-click="_copy($event,$index)" data-ng-model="data.regions[$index].keywords"> <option value="">Use the same keywords as:</option> <option ng-repeat="region in data.regions | myFilter" value="{{region.keywords}}">{{region.name}}</option> </select> </div> 使用Javascript: app.filter('myFilter',function() { return function(items) { var group = []; for (var i = 0; i < items.length; i++) { group.push(items.name); } for (var i = 0; i < group.length; i++) { group[i].splice(i,1); } return group; }; });
使选择不包括当前选择的内容存在缺陷.我将在解决方案中包含错误,以便您可以看到它.
ng-options是你需要的.以下是使用2个单独选择的示例. <select ng-model='selectedCountry' ng-options='country.name for country in countries'></select> <select ng-model='selectedCountry' ng-options='country.name for country in filteredCountries()'></select> 这是$scope件.请注意,filteredCountries只是返回国家/地区,减去所选国家/地区. $scope.countries = [{name:'United States'},{name:'United Kingdom'},{name:'Canada'}]; $scope.filteredCountries = function(){ var filteredCountries = []; angular.forEach($scope.countries,function(val,key){ if(val !== $scope.selectedCountry){ this.push(val); } },filteredCountries); return filteredCountries; }; 您会注意到的错误是,只要您选择第二个,它就不会显示您选择的值,因为它会立即从选项中过滤掉.这不是最好的用户体验. 工作范例: 更新: 而不是上面列出的filteredCountries函数,手表将更有效.它只会在检测到监视值发生变化时运行代码. $scope.$watch('version1model',function(){ $scope.filteredItems = []; angular.forEach($scope.items,key){ if(val.id != $scope.version1model){ this.push(val); } },$scope.filteredItems); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 使用AngularJs处理Play scala发送的分块数据
- First Angular2 App with TypeScript and Visual Studio 20
- vim – :排序你 – 但只在csv中的一列?
- 网上常用免费WebServices集合
- 干货|使用AngularJS+nodejs实现聊天室
- varnish服务yum安装及不同域名站点
- angularjs – Dygraphs不使用ng-repeat
- bash – SCP用美元符号覆盖文件
- scala – 为什么`trait X {def append [ – ]():Unit}`编译
- Bootstrap typeahead不显示AJAX结果