gpt4 book ai didi

javascript - 如果子数组的长度大于零,则显示对象

转载 作者:行者123 更新时间:2023-12-03 04:53:28 24 4
gpt4 key购买 nike

<select id="type" required name="type" ng-options="type.Name for type in types| filter:{Active:true} | filter:subTypeFilter(type)"><option value="" disabled selected>Choose your option</option>

$scope.subTypeFilter = function (type) {
return function (item) {
return item.SubTypes.length > 0;
}
}

我试图仅显示“类型”,其中“SubType”数组为 null 或长度大于 0。

它不起作用,想知道是否有人知道我做错了? :)

谢谢。

最佳答案

您应该有一个 ng-model 才能使其正常工作。

由于您使用的是 ng-repeat Angular Directive(指令),因此它会搜索 ng-model

请运行以下代码

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">


<select id="type" ng-model="typeSelected" required name="type" ng-options="type.name for type in types | filter:subTypeFilter(type)"><option value="" disabled selected>Choose your option</option>


</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.types = [{
"name": "Alabama",
"SubTypes": []
}, {
"name": "Alaska",
"SubTypes": ["USA"]
}, {
"name": "Arizona",
"SubTypes": ["USA"]
}, {
"name": "Alberta",
"SubTypes": ["USA"]
}, {
"name": "British columbia",
"SubTypes": []
}];
$scope.subTypeFilter = function (type) {
return function (item) {
console.log(item)
return item.SubTypes.length > 0;
}
}
});
</script>

<p>This example shows how to fill a dropdown list using the ng-options directive.</p>

</body>
</html>

Here is a working DEMO

关于javascript - 如果子数组的长度大于零,则显示对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42548609/

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