【问题标题】:Angular ng-options with conditional on value in simple array of strings以简单字符串数组中的值为条件的 Angular ng-options
【发布时间】:2017-01-30 19:36:54
【问题描述】:

我有两个这样的数组:

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

我尝试像这样使用 ng-options:

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

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

【问题讨论】:

    标签: javascript angularjs filter conditional


    【解决方案1】:

    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:

    如果你想给过滤函数传递一个额外的参数

    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

    【讨论】:

    • 如果我想添加一个额外的参数,在我的情况下,我希望它能够根据传递的 int 跳过它,我怎样才能将此参数添加到函数中? (使用1个参数调用函数并将其添加到函数中不起作用)
    • @schuimer 我已经更新了我的帖子,为您的问题提供了解决方案。
    • 像魅力一样工作!非常感谢!
    【解决方案2】:

    使用filter 怎么样?喜欢

        <select ng-options="value as value for value in (values_array | yourFilter)"></select>
    

    yourFilter 可能与this answer 类似:

    .filter('inArray', function($filter){
        return function(list, arrayFilter, element){
            if(arrayFilter){
                return $filter("filter")(list, function(listItem){
                    return arrayFilter.indexOf(listItem[element]) != -1;
                }); 
            }
        };
    });
    

    【讨论】:

      【解决方案3】:

      只需使用将返回过滤值的function。您也可以使用单独的filter,但在这种情况下function 应该更容易

       <select name="mySelect" id="mySelect" ng-model="mySelectmodel" ng-options="value as value for value in filteredValues(values_array)">
      
      $scope.filteredValues = function () {
          return $scope.values_array.filter(function (val) {
              return $scope.skip_array.indexOf(val) === -1;
              });
          };
      

      这是完整的工作示例

      FULL EXAMPLE

      【讨论】:

        【解决方案4】:

        HTML

        <select ng-modal="yourmodalname" ng-options="x.id as x.name for x in arraylistofvalue | filter : selectedvalues"></select
        

        角度控制器

         $scope.selectedvalues = function(value , index , array ){
             if (value.id == 1){
                    return $scope.arraylistofvalue.indexOf(value);
                 }
        
              }
        

        数组列表

        $scope.arraylistofvalue = [{id : 1 ,name : 'value1'}{id : 2 ,name : 'value2'}]
        

        【讨论】:

          猜你喜欢
          • 2014-09-11
          • 1970-01-01
          • 2017-04-29
          • 2016-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-01
          相关资源
          最近更新 更多