gpt4 book ai didi

angularjs - 在 AngularJS 中添加 optgroups 被证明是很棘手的

转载 作者:行者123 更新时间:2023-12-02 17:03:02 26 4
gpt4 key购买 nike

我正在使用 angularjs,过去 2 天我一直在尝试创建一个 optgroup,同时处理其他一些东西,但我被难住了。我必须创建一个 ng-options,但没有考虑 optgroup,直到我意识到我需要一个。我尝试使用 ng-options 添加它,但它要么完全忽略它,弄乱代码,要么完全忽略选项。

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

我尝试的一切都会破坏我的代码。原始代码如下(我的许多其他 mods 之前的代码),但我也有它的 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>

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' }
];
}

};
});


})();

最佳答案

不确定您想要按什么对它们进行分组,因此我添加了一个额外的属性type:

语法:

ng-options="option.name as option.value group by option.type for option in options"

DEMO

关于angularjs - 在 AngularJS 中添加 optgroups 被证明是很棘手的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24811461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com