gpt4 book ai didi

javascript - 如何在 ng-repeat 中显示数组的特定字母表项?

转载 作者:技术小花猫 更新时间:2023-10-29 11:47:26 24 4
gpt4 key购买 nike

我有两个数组,一个用于字母表,第二个用于标签。所以我只想显示该特定字母类别中的相同字母元素示例 -- 苹果必须归入 A 类,而动物园必须归入 Z 类...

fiddle 链接 http://jsfiddle.net/4dGxn/149/

html

<div class="form-group" ng-controller="mainCtrl">
<div ng-repeat="alp in alpha">
<h2>{{alp}}</h2>
<ul>
<li ng-repeat="tag in tags">{{tag}}</li>
</ul>
</div>
</div>

Angular

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

app.controller("mainCtrl", ['$scope', function($scope){

$scope.alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p","q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

$scope.tags = ["apple", "dog", "cat", "dad", "baby", "zoo", "love", "hate", "rat", "room", "home", "age", "bad"];

}]);

最佳答案

您可以使用 str.startsWith 来检查元素是否以特定字符开头所以在你的例子中:

<div class="form-group" ng-controller="mainCtrl">
<div ng-repeat="alp in alpha">
<h2>{{alp}}</h2>
<ul>
<li ng-repeat="tag in tags" ng-show="{{tag.startsWith(alp)}}">{{tag}}</li>
</ul>
</div>
</div>

这里是 wirking Fiddle

关于javascript - 如何在 ng-repeat 中显示数组的特定字母表项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36127719/

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