gpt4 book ai didi

javascript - 获取 ng-repeat 的匹配 ng-if 的数量

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

假设我有类似的东西:

<md-grid-tile class="gray" ng-repeat="carto in cartoList" ng-if="search(carto)">
<md-button ng-click="changeSVG(carto.fileName)" aria-label="carto.displayName">
<img src="style/images/thumbnails/{{carto.fileName}}.png" width="100%" height="100%" title="{{carto.fullDisplayName}}" style="max-height: 220px;"></img>
</md-button>
<md-grid-tile-footer><h3 align="center">{{carto.displayName}}</h3> </md-grid-tile-footer>
</md-grid-tile>

有没有办法检索显示的图 block 数量?表示 ng-repeat 中的元素数量匹配ng-if :

<md-grid-tile class="gray" ng-repeat="carto in cartoList" ng-if="search(carto)">

根据要求,search功能:

    $scope.search = function (carto) {
if ($scope.sideMenu) {
var searchRegex = new RegExp('global', 'i');

if (carto.fullDisplayName.search(searchRegex) != -1)
return true;
}
else if ($scope.sideSearch) {
if ($scope.searched.IS.indexOf(carto.informationSystem) === -1 && carto.informationSystem)
return false;
if ($scope.searched.area.indexOf(carto.area) === -1 && carto.area)
return false;
if ($scope.searched.block.indexOf(carto.block) === -1 && carto.block)
return false;
if ($scope.searched.type.indexOf(carto.type) === -1 && carto.type)
return false;
if ($scope.searched.level.indexOf(carto.level) === -1 && carto.level)
return false;

if ($scope.searchInput) {
var searchRegex = new RegExp($scope.searchInput, 'i');

if (carto.fullDisplayName.search(searchRegex) === -1)
return false;
}
if (!$scope.homeVisible) {
$scope.homeVisible = true;
window.history.pushState("newUrl", "CartoViewer", window.location.origin + window.location.pathname);
}
return true;
}
else
return true;
};

最佳答案

就我个人而言,我会在将 cartoList 发送到 View 之前过滤:

$scope.cartoListFiltered = function() {
return $scope.cartoList.filter($scope.search);
};

在您看来:

<md-grid-tile class="gray" ng-repeat="carto in cartoListFiltered()">
<md-button ng-click="changeSVG(carto.fileName)" aria-label="carto.displayName">
<img src="style/images/thumbnails/{{carto.fileName}}.png" width="100%" height="100%" title="{{carto.fullDisplayName}}" style="max-height: 220px;"></img>
</md-button>
<md-grid-tile-footer><h3 align="center">{{carto.displayName}}</h3> </md-grid-tile-footer>
</md-grid-tile>

这会针对每个单独的 carto 运行您的 $scope.search,并且仅包含返回 true 的 carto。与 ng-if 相同,只不过逻辑是在 Controller 上执行的(应该在的位置)

关于javascript - 获取 ng-repeat 的匹配 ng-if 的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31183376/

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