当前位置: 首页 > 面试题库 >

如何在带有ng-options的select中使用ng-class

萧光华
2023-03-14
问题内容

我有一个Person对象数组

var persons = [
{Name:'John',Eligible:true},
{Name:'Mark',Eligible:true},
{Name:'Sam',Eligible:false},
{Name:'Edward',Eligible:false},
{Name:'Michael',Eligible:true}
];

我正在使用带有ng-options这样的select:

<select ng-model="Blah" ng-options="person.Name for person in persons"></select>

我想显示与记录 符合条件的:假 颜色。因此,问题是我如何使用ng-classin
select才能实现此目的?因为我们没有使用任何option标签,如果我只需添加它不会工作ng-classselect元素本身。


问题答案:

您可以在处理ngOptions指令后创建一个处理选项的指令,以适当的类更新它们。

更新
:旧代码有一些错误,并且自回答这个问题以来,我学到了一点。这是在1.2.2中重做的Plunk(但也应在1.0.X中工作)

此处 更新 了代码 (13年11月30日在3:17)

app.directive('optionsClass', function ($parse) {
  return {
    require: 'select',
    link: function(scope, elem, attrs, ngSelect) {
      // get the source for the items array that populates the select.
      var optionsSourceStr = attrs.ngOptions.split(' ').pop(),
      // use $parse to get a function from the options-class attribute
      // that you can use to evaluate later.
          getOptionsClass = $parse(attrs.optionsClass);

      scope.$watch(optionsSourceStr, function(items) {
        // when the options source changes loop through its items.
        angular.forEach(items, function(item, index) {
          // evaluate against the item to get a mapping object for
          // for your classes.
          var classes = getOptionsClass(item),
          // also get the option you're going to need. This can be found
          // by looking for the option with the appropriate index in the
          // value attribute.
              option = elem.find('option[value=' + index + ']');

          // now loop through the key/value pairs in the mapping object
          // and apply the classes that evaluated to be truthy.
          angular.forEach(classes, function(add, className) {
            if(add) {
              angular.element(option).addClass(className);
            }
          });
        });
      });
    }
  };
});

这是在标记中使用它的方式:

<select ng-model="foo" ng-options="x.name for x in items" 
        options-class="{ 'is-eligible' : eligible, 'not-eligible': !eligible }">
</select>

它像ng-class一样工作,不同之处在于它是基于集合中的每个项目。



 类似资料:
  • 问题内容: 是否有可能根据标准将其呈现为禁用的行? 这个: 也许有可能变成这样: 假设通过一个过滤器,该过滤器可以返回所有具有shade =“ dark”的颜色 问题答案: @lucuma的答案(最初是可接受的答案)是正确的,但是现在应该更新,因为这已在Angular 1.4中修复。请参阅ng- options 的文档,其中也包含一个示例。 我正在使用Angular 1.5,这对我有用: 视图 控

  • 我在其他的帖子里读到过,但我想不通。 我读过select也试过,但我想不通。

  • 问题内容: 我希望解决三个问题… 在我的应用程序页面中,我有一个选择州,另一个选择县。对于州,我有: 数据: 对于我的县,请选择: 数据: 这是我的: 因此,如您所见,我在州选择上使用,在县选择上使用,在县数据集中已设置了相应的州ID 。 我想做几件事: 隐藏选择的县,直到选择了一个州。 选择州后,按选定的/ 过滤县选择选项 筛选由第一,然后由。 我还没有看到一个方法来设置到或过滤器由一个数字,而

  • 问题内容: 有一个基于任何数组的。数组中的元素可能会更改。如何获得角度控制器来刷新阵列? module.js index.html 从控制台: 如何刷新以使其中的所有元素都是选项? 问题答案: Angular使用watchers,并且只有在摘要循环开始后才会更新UI 。 通常,您将通过UI中的某个事件或调用$ http服务 将数组添加到数组中,而这些将为您启动$ digest() 。 由于您只是直

  • 我想比较一下id,这里,如果id等于5,做这个,否则做那个。我怎样才能做到这一点?

  • ng-select 是 Angular 6+ 原生的下拉框组件。 主要特性:  自定义属性和对象的绑定  自定义选项、文本标签、头部和底部模板  虚拟滚动支持,用于处理超过 5000 个条目的数据集  无限滚动  键盘访问  多选项支持  灵活的自动完成,支持客户端和服务器端的过滤  自定义搜索  自定义标签  条目追加  条目分组  输出事件  五障碍使用  良好的测试  支持不同主题