简单字符串数组中条件值为值的角ng-options [英] Angular ng-options with conditional on value in simple array of strings

查看:59
本文介绍了简单字符串数组中条件值为值的角ng-options的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个形状像这样的数组:

I have two array shaped like this:

$scope.values_array = ['string1', 'string2', 'string3', 'string4'];
$scope.skip_array = ['string2', 'string3'];

,我尝试使用ng-options如下:

and I try to use ng-options like this:

<select ng-options="value as value for value in values_array && skip_array.indexOf(value) === -1"></select>

这不会产生错误,但下拉菜单中没有任何显示。没有&& skip_array.indexOf(value),它将显示values_array中的所有选项。

This doesn't produce an error but nothing shows up in the dropdown anymore. Without the "&& skip_array.indexOf(value)" it shows all the options in values_array.

推荐答案

html:

<select ng-model="selectedValue" ng-options="value as value for value in values_array | filter:skipValues"></select>

控制器:

$scope.skipValues = function(value, index, array) {
    return $scope.skip_array.indexOf(value) === -1;
};
$scope.values_array = ['string1', 'string2', 'string3', 'string4'];
$scope.skip_array = ['string2', 'string3'];

jsfiddle

UDPATE:

如果需要将额外的参数传递给过滤器功能

If you want to pass an extra parameter to the filter function

html:

<select ng-model="selectedValue" ng-options="value as value for value in values_array | filter:skipValues(1)"></select>

控制器:

$scope.skipValues = function(anInt) {
    return function(value, index, array) {
        return $scope.skip_array.indexOf(value) === -1 && anInt > 0;
    }
};
$scope.values_array = ['string1', 'string2', 'string3', 'string4'];
$scope.skip_array = ['string2', 'string3'];

jsfiddle

这篇关于简单字符串数组中条件值为值的角ng-options的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆