将自定义类添加到角度选定的容器

时间:2014-07-26 03:28:32

标签: javascript jquery angularjs jquery-chosen

代码:http://plnkr.co/edit/H2hmEukfjaPL1T4W298O?p=preview

HTML

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <link data-require="chosen@*" data-semver="1.0.0" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.min.css" />
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.20/angular.js" data-semver="1.2.20"></script>
    <script src="chosen.jquery.js"></script>
    <script src="chosen.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>

    <select chosen class="red" ng-model="bar">
      <option>Hi</option>
      <option>This is fun</option>
      <option>I like Chosen so much</option>
      <option>I also like bunny rabbits</option>
      <option value=""></option>
    </select>
  </body>

</html>

JS

var app = angular.module('plunker', ['localytics.directives']);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

您可以看到我尝试将类red添加到由角度选择创建的div以及其他类chosen-container chosen-container-single。我不想手动更改这些类。

确保添加其他课程的正确方法是什么?

由于

1 个答案:

答案 0 :(得分:5)

看起来chosen.jquery.js的第153行有inherit_select_classes的选项,默认为false

chosen.js中指令的第39行查看chosen的{​​{1}}属性:

options

所以制作你的选择标记:

options = scope.$eval(attr.chosen) || {};

导致生成:

<select chosen="{inherit_select_classes:true}" class="red" ng-model="bar">

(省略与此问题无关的额外属性)