Angular.js - 使用 ng-options 将类添加到选项

2024-04-14

有人问过类似的问题(如何在带有 ng-options 的 select 中使用 ng-class https://stackoverflow.com/questions/15264051/how-to-use-ng-class-in-select-with-ng-options),但我也添加了我的,因为它与其他人的问题的答案有关。

解决方案很棒,但我不太明白。

答案是创建一个指令 -http://plnkr.co/edit/rbc4GWBffi4eFYhbvS6u?p=preview http://plnkr.co/edit/rbc4GWBffi4eFYhbvS6u?p=preview.

我想做同样的事情,但添加的类应该与 items.name 相同。我怎么做?

console.clear();

var app = angular.module('angularjs-starter', []);

app.controller('MainCtrl', function($scope) {
  $scope.items = [
    { name: 'foo', id: 1, eligible: true },
    { name: 'bar', id: 2, eligible: false },
    { name: 'test', id: 3, eligible: true }
    ];
});

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);
            }
          });
        });
      });
    }
  };
});
/* CSS goes here */
.is-eligible {
  color: green;
}
.not-eligible {
  color: red;
}
<!DOCTYPE html>
<html ng-app="angularjs-starter">
  
  <head lang="en">
    <meta charset="utf-8">
    <title>Custom Plunker</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.min.js"></script>
    <link rel="stylesheet" href="style.css">
    <script>
      document.write('<base href="' + document.location + '" />');
    </script>
    <script src="app.js"></script>
  </head>
  
  <body ng-controller="MainCtrl">
    <select ng-model="foo" ng-options="x.name for x in items" 
            options-class="{ 'is-eligible' : eligible, 'not-eligible': !eligible }"></select>
  </body>

</html>

提前致谢


将此逻辑硬编码到指令中的一种方法是使用option.text():

      angular.element(option).addClass(option.text()); // 

但是,这会忽略该表达式。http://plnkr.co/edit/46HndjYtg6HUbblnceNr?p=preview http://plnkr.co/edit/46HndjYtg6HUbblnceNr?p=preview

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular.js - 使用 ng-options 将类添加到选项 的相关文章

随机推荐