Angular 1:带有多个条件的多个条件,或者如何在其他条件为真时排除条件

Angular 1: multiple conditions with multiple conditions OR how to exclude conditions if other conditions are true

本文关键字:条件 其他 排除 Angular 或者      更新时间:2023-09-26

现在我已经创建了一个额外的span来容纳一个条件。

<div ng-repeat="(key, resultLinks) in resultGroup.resultLinks">
    <div ng-if="key < 4 || viewMore" ng-repeat="(subKey, linksWrap) in resultLinks.linksWrap">
        <span ng-if="wWidth > 568 || subKey == 0" ng-repeat="links in linksWrap.links">
                {{links.linkName}}
        </span>
    </div>
</div>

你会如何改变它,使所有的条件都在一个div。4 || viewMore) || (wWidth> 568 || subKey == 0)"(有点像这样,但不完全是这样)。比如,如果另一个条件为真是否有可能以某种方式排除一个条件?

这是你要找的吗?

(key < 4 || viewMore) && (wWidth > 568 || subKey == 0)

是否有可能以某种方式排除一个条件,如果另一个条件为真?

这正是OR (||)算子所做的。如果(key < 4 || viewMore)true,它不会继续执行,(wWidth > 568 || subKey == 0)不会执行。

这个应该可以工作:

    <div ng-if="(key < 4 || viewMore) || (wWidth > 568 || subKey == 0)" ng-repeat="(subKey, linksWrap) in resultLinks.linksWrap">
        <span ng-repeat="links in linksWrap.links">
               {{links.linkName}}
        </span>
    </div>

你可以看到它看起来很丑。我将把逻辑移出模板:

<div ng-if="isVisible(key, subKey)"... >

在你的控制器的某处:

$scope.isVisible = function(key, subKey){  
    return (key < 4 || $scope.viewMore) || ($scope.wWidth > 568 || subKey == 0)
}