javascript - 根据 angularJS 中的条件显示链接

标签 javascript angularjs json variable-length-array angularjs-ng-if

我正在 AngularJS 中创建一个导航栏。 我在“div.menu-links”上分别在鼠标悬停和鼠标离开时显示和隐藏子菜单“div.farm-links”。

现在,在我的子菜单中,每当“child.thirdLevelChildList.length”大于 5 时,它应该将第 5 个链接显示为“查看全部”并隐藏所有剩余链接。否则,它应该显示所有链接。

例如,鼠标悬停在“关于 abc”上,在“公司”下,我应该只看到:“战略”、“使命、愿景、值(value)观”、“领导力”、“组织结构图”和第 5 个链接为“查看全部'。

我如何实现这一点? 这是我的代码:

angular.module('topNavApp', ['ngAnimate']).controller('navCtrl', ['$scope', '$timeout', function($scope, $timeout){
  $scope.pageData = {  
    "allChildList":[  
      {  
         "pageExist":true,
         "pageTitle":"About Abc",
         "pagePath":"http://www.yahoo.com",
         "openNewTab":true,
         "hideInTabletNav":true,
         "hideInMobileNav":true,
         "secondLevelChildList":[  
            {  
               "pageExist":true,
               "pageTitle":"News",
               "pagePath":"/content/myloc/about-Abc/news.html",
               "openNewTab":false,
               "thirdLevelChildList":[  

               ],
               "multiple":true,
               "thirdLevelPageExist":true
            },
            {  
               "pageExist":true,
               "pageTitle":"Company",
               "pagePath":"/content/myloc/about-Abc/company.html",
               "openNewTab":false,
               "thirdLevelChildList":[  
                  {  
                     "pageExist":true,
                     "pageTitle":"Strategy",
                     "pagePath":"/content/myloc/about-Abc/company/strategy.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Mission Vision Values",
                     "pagePath":"/content/myloc/about-Abc/company/missionvisionvalues.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Leadership",
                     "pagePath":"/content/myloc/about-Abc/company/leadership.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Org Chart",
                     "pagePath":"http://mysite/pages/OrgChart.aspx",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Corporate Responsibility",
                     "pagePath":"http://myloc2/who/global-impact",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"History",
                     "pagePath":"https://usa.Abc.com/about-Abc/our_business/history-of-Abc.html",
                     "openNewTab":true
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Products",
                     "pagePath":"http://myloc2/who/products",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Investor Info",
                     "pagePath":"http://investor.Abc.com/",
                     "openNewTab":true
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"View All",
                     "pagePath":"/content/myloc/about-Abc/company/view-more.html",
                     "openNewTab":false
                  }
               ],
               "multiple":true,
               "thirdLevelPageExist":true
            },
            {  
               "pageExist":true,
               "pageTitle":"Community",
               "pagePath":"/content/myloc/about-Abc/community.html",
               "openNewTab":false,
               "thirdLevelChildList":[  
                  {  
                     "pageExist":true,
                     "pageTitle":"Diversity + Inclusion",
                     "pagePath":"/content/myloc/about-Abc/community/diversity-and-inclusion.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Giving Back",
                     "pagePath":"http://myloc2/working/giving-back/Pages/default.aspx",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Ideas Community",
                     "pagePath":"/content/myloc/about-Abc/initiatives/ideas.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Other Communities",
                     "pagePath":"http://myloc2/working/get-involved/Pages/default.aspx",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"View All",
                     "pagePath":"/content/myloc/about-Abc/community/view-all-community.html",
                     "openNewTab":false,
                     "hideInDesktopNav":true
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"myloc Transformation",
                     "pagePath":"/content/myloc/about-Abc/community/transformation.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Calendar",
                     "pagePath":"/content/myloc/about-Abc/community/Calendar.html",
                     "openNewTab":false
                  }
               ],
               "multiple":true,
               "thirdLevelPageExist":true
            },

         ],
         "columnClass":"0",
         "multiple":true,
         "secondLevelPageExist":true
      },
      {  
         "pageExist":true,
         "pageTitle":"Offices",
         "pagePath":"http://www.yahoo.com",
         "openNewTab":true,
         "hideInDesktopNav":true,
         "hideInTabletNav":true,
         "hideInMobileNav":true,
         "secondLevelChildList":[  
            {  
               "pageExist":true,
               "pageTitle":"Locations",
               "pagePath":"/content/myloc/offices/locations.html",
               "openNewTab":false,
               "thirdLevelChildList":[  
                  {  
                     "pageExist":true,
                     "pageTitle":"Ashburn",
                     "pagePath":"/content/myloc/offices/locations/ashburn.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Austin",
                     "pagePath":"/content/myloc/offices/locations/austin.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Bangalore",
                     "pagePath":"/content/myloc/offices/locations/bangalore.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Denver",
                     "pagePath":"/content/myloc/offices/locations/denver.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Dubai",
                     "pagePath":"/content/myloc/offices/locations/dubai.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Foster City",
                     "pagePath":"/content/myloc/offices/locations/foster-city.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"San Francisco",
                     "pagePath":"/content/myloc/offices/locations/san-francisco.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Miami",
                     "pagePath":"/content/myloc/offices/locations/miami.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Singapore - Singpost",
                     "pagePath":"/content/myloc/offices/locations/singapore-singpost.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Singapore - Robinson Road",
                     "pagePath":"/content/myloc/offices/locations/singapore-robinson-road.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"View All",
                     "pagePath":"http://myloc.trusted.Abc.com/content/myloc/offices/locations.html",
                     "openNewTab":true
                  }
               ],
               "multiple":true,
               "thirdLevelPageExist":true
            },
            {  
               "pageExist":true,
               "pageTitle":"Quick Links",
               "pagePath":"/content/myloc/offices/quick-links.html",
               "openNewTab":true,
               "hideInDesktopNav":true,
               "hideInMobileNav":true,
               "thirdLevelChildList":[  
                  {  
                     "pageExist":true,
                     "pageTitle":"Copy Centers",
                     "pagePath":"/content/myloc/offices/quick-links/copy-centers.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Floor Maps",
                     "pagePath":"/content/myloc/offices/quick-links/floor-maps.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"Food Services",
                     "pagePath":"/content/myloc/offices/quick-links/food-services.html",
                     "openNewTab":false
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"View All",
                     "pagePath":"/content/myloc/offices/quick-links/view-all.html",
                     "openNewTab":false
                  }
               ],
               "multiple":true,
               "thirdLevelPageExist":true
            },
            {  
               "pageExist":true,
               "pageTitle":"Facilities Help",
               "pagePath":"/content/myloc/offices/facilities-help.html",
               "openNewTab":false,
               "thirdLevelChildList":[  
                  {  
                     "pageExist":true,
                     "pageTitle":"Submit Request",
                     "pagePath":"http://servicerequest",
                     "openNewTab":true
                  },
                  {  
                     "pageExist":true,
                     "pageTitle":"View All",
                     "pagePath":"/content/myloc/offices/facilities-help/view-all-facilities-help.html",
                     "openNewTab":false,
                     "hideInDesktopNav":true
                  }
               ],
               "multiple":true,
               "thirdLevelPageExist":true
            },
            {  
               "pageExist":true,
               "pageTitle":"View All",
               "pagePath":"/content/myloc/offices/view-all-offices.html",
               "openNewTab":false,
               "hideInDesktopNav":true,
               "thirdLevelChildList":[  

               ],
               "thirdLevelPageExist":false
            }
         ],
         "columnClass":"3",
         "multiple":true,
         "secondLevelPageExist":true
      },
      {  
         "pageExist":true,
         "pageTitle":"Functions",
         "pagePath":"https://www.google.com",
         "openNewTab":true,
         "hideInDesktopNav":true,
         "hideInTabletNav":true,
         "hideInMobileNav":true,
         "secondLevelChildList":[  

         ],
         "columnClass":"1",
         "multiple":true,
         "secondLevelPageExist":true
      },
      {  
         "pageExist":true,
         "pageTitle":"My HR",
         "pagePath":"/content/myloc/my-hr.html",
         "openNewTab":false,
         "secondLevelChildList":[  

         ],
         "columnClass":"1",
         "multiple":true,
         "secondLevelPageExist":true
      },
      {  
         "pageExist":true,
         "pageTitle":"Tools",
         "pagePath":"/content/myloc/tools.html",
         "openNewTab":true,
         "secondLevelChildList":[  

         ],
         "columnClass":"1",
         "multiple":true,
         "secondLevelPageExist":true
      }
   ]
};
  $scope.showDiv = function(obj){
	$timeout(function(){
	  obj.hovering = true;
	}, 500);
		
  };
  $scope.hideDiv = function(obj){
	$timeout(function(){
	  obj.hovering = false;
	 }, 500);
  };
}]);
.hiding-div{
	display: inline-block;
	padding: 10px;
	margin: 10px;
}

.menu-link{
	text-decoration: none;
}

.menu-link:hover{
	color: #AB09AF;
}

.farm-links{
	position: absolute;
	top: 50px;
	height: 400px;
	width: 700px;
	border: 1px solid lightgrey;
	padding: 5px;
	display: flex;
	word-wrap: flex;
	column-count: 4;
}


.groups-links{
	display: inline-block;
	margin-right: 60px;

}

.group-title{
	color: grey;
	text-decoration: none;
}

.group-links{
	list-style: none;
}

.second-link{
	margin-left: -40px;
}

.second-link > a{
	text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<nav ng-app="topNavApp" ng-controller="navCtrl" class="nav">
  <div class="nav-center">
	<!--<li ng-repeat="obj in pageData.allChildList" ng-model="parentNav" ng-mouseover="parentNav=true" ng-mouseleave="parentNav=false"> -->
	<div ng-repeat="obj in pageData.allChildList" class="hiding-div" ng-mouseover="showDiv(obj)" ng-mouseleave="hideDiv(obj)" > 
	  <div>
		<a ng-href="{{obj.pagePath}}" class="main-link multiple menu-link">{{obj.pageTitle}}</a>
		<!--<span class="main-link mobile" aria-labelledby="{{obj.pageTitle}}" aria-expanded="false">{{obj.pageTitle}}</span>-->

		<!--<span ng-repeat="child in obj.secondLevelVoList" class="childNav" ng-show="parentNav">--> 
		<div class="farm-links" ng-show="obj.hovering">
		  <!--<a class="prev-link" aria-labelledby="{{obj.pagetitle}}">{{obj.pageTitle}}</a>-->
		  <div ng-repeat="child in obj.secondLevelChildList" class="groups-links">
			<a ng-href="{{child.pagePath}}" class="group-title">{{child.pageTitle}}</a>
			<!--<span class="group-title mobile" aria-expanded="false">{{child.pageTitle}}</span>-->
			<ul ng-repeat="subchild in child.thirdLevelChildList" class="group-links">
			  <li class="second-link">
				<a ng-href="{{subchild.pagePath}}">{{subchild.pageTitle}}</a>
			  </li>
			</ul>
		  </div>
		</div>
	  </div>
	</div>
  </div>
</nav>

最佳答案

我相信你需要的是这个:

  <ul class="group-links">
    <li class="second-link" ng-repeat="subchild in child.thirdLevelChildList | limitTo:4">
      <a ng-href="{{subchild.pagePath}}">{{subchild.pageTitle}}</a>
    </li>
    <li>
      <a href="#" ng-if="child.thirdLevelChildList.length > 4">Show All</a>
    </li>
  </ul>

注意:您在 <ul> 上进行了 ng-repeat| .我假设您想重复列表项,而不是列表(基于 thirdLevelChildList 中的值,所以我以这种方式实现。

我使用 limitTo以防止显示超过 4 个项目。 我用 ng-if仅在大于 4 时显示“全部显示”。

关于javascript - 根据 angularJS 中的条件显示链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45574360/

相关文章:

javascript - 在多个元素上使用 addEventListener,避免在未找到特定元素时出现 TypeError

AngularJS全局修改$http中每个请求的URL

javascript - 如何在$q下传递多个$http promise

java - Node.js 套接字上的数据太多?

javascript - 使用突出显示插件匹配精确单词单个或多个单词

javascript - jQuery - 设置计算的 div 宽度

javascript - 使用 Ionic - 无法正确加载 Firebase - 错误 : Module 'firebase' is not available

json - 在不知道Golang中结构的情况下,在JSON文件中编辑值

json - 如何在 JSONPath 中按字符串过滤?

javascript - 如何将 CJK 文本拆分为单词?