在angularjs中添加optgroups证明是棘手的

use*_*600 15 optgroup angularjs

我正在使用angularjs,我一直在努力创建一个过去2天的optgroup,同时处理其他一些东西,我很难过.我不得不创建一个ng-options,但直到我意识到我需要一个选项组才考虑.我尝试使用ng-options将其添加进去,但它会完全忽略它,弄乱代码或完全忽略选项.

我不知道我是否真的可以使用(双)&符号,所以我尝试结合两个ng-options,但无济于事.此外,我玩弄了odetocode中的特定代码(这似乎很有希望,但由于其自身有限的结构,无法与我的代码合并).

我试过的一切都破坏了我的代码.原始代码在下面(我的许多其他mod之前的代码),但我也有它的plunker:

http://plnkr.co/edit/xCLe2GFShUMUlIySPLH9?p=info

HTML

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

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
    <script src="script.js"></script>
  </head>

  <body>


  <form name="FORM" ng-controller="appController">
<the-ratings  model="review.ratings">         
</the-ratings>
</form>

  </body>

</html>
Run Code Online (Sandbox Code Playgroud)

JS

(function(){

var app = angular.module('myApp',[]);

app.controller('appController', ['$scope', function($scope) {
      $scope.review = {};
      $scope.review.ratings = '5 stars';
  }])
  app.directive('theRatings', function() {

    return {
      restrict: 'E',
      scope: { model: '=' },
      template: '<select ng-model="model" ng-options="option.name as option.value for option in options"></select>',
      controller: function($scope) {
        $scope.options = [
            { name:'test', namegroup: 'Rate the product', value:'test2',valueName: 'NA' },
            { name: '1 star', value: '1 star' }, 
            { name: '2 stars', value: '2 stars' }, 
            { name: '3 stars', value: '3 stars' },
            { name: '4 stars', value: '4 stars' },
            { name: '5 stars', value: '5 stars' }
        ];
      }

    };
  });


})();
Run Code Online (Sandbox Code Playgroud)

cha*_*tfl 24

不确定你想要将它们分组,所以我添加了一个额外的属性type:

句法:

ng-options="option.name as option.value group by option.type for option in options"
Run Code Online (Sandbox Code Playgroud)

DEMO


zet*_*tic 5

AngularJS文档中有一个很好的例子,如果您想对选项的属性进行分组,则可以添加“ group by”子句。例如:

$scope.options = [
  { name:'test', namegroup: 'Rate the product', value:'test2',valueName: 'NA' },
  { type: 'one', name: '1 star', value: '1 star' }, 
  { type: 'one', name: '2 stars', value: '2 stars' }, 
  { type: 'two', name: '3 stars', value: '3 stars' },
  { type: 'two', name: '4 stars', value: '4 stars' },
  { type: 'two', name: '5 stars', value: '5 stars' }
];
Run Code Online (Sandbox Code Playgroud)

以及ng-options:

"option.name as option.value group by option.type for option in options"
Run Code Online (Sandbox Code Playgroud)