简体   繁体   中英

Hide or Display an Item with Angular or Jquery?

i am using mvc. I have model and i take data from model to view with this code:

<ul>
   <li id="geri"><<</li>

   @foreach (var item in Model.Skills)
   {
      <li id="@String.Format("{0}{1}", "skill", item.SkillId)">
         @item.SkillName
      </li>
   }

   <li id="ileri" style="margin-right: 0;">>></li>
</ul>

After first 4 items, they should be hidden (display:none). I searched angular and find ng-show attribute but cannot find how to use. Now my website looks like:

It should be one line and when i pressed next button, first item will hide and 5th item will show.

I hope i can explain myself, thanks

In Angular your HTML should be something like this to display only the first 4 items <li> , where items is your $scope.items :

<ul>
   <li id="geri"><<</li>
   <li ng-repeat="(key, item) in items" ng-show="key <= 3">{{item.SkillName}}</li>
   <li id="ileri" style="margin-right: 0;">>></li>
</ul>

JSFiddle here

In Angular, try to use limitTo and offset filters.

Here's the Jsfiddle link .

AngularJS sample codes:

HTML :

<div ng-app="myApp">
    <ul ng-controller="YourCtrl">
       <li ng-click="previousSkills()"><<</li>
       <li ng-repeat="skill in skills | offset: currentPage * 4 | limitTo: 4">
           {{skill.SkillName}}
        </li>
       <li ng-click="nextSkills()">>></li>
    </ul>
</div>

AngularJS Controller :

'use strict';

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

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

    $scope.currentPage = 0;

    $scope.skills = [
        {SkillName:'C#'},
        {SkillName:'MVC'},
        {SkillName:'Web Forms'},
        {SkillName:'Web API'},
        {SkillName:'SignalR'},
        {SkillName:'EF'},
        {SkillName:'Linq'},
        {SkillName:'Github'},
        {SkillName:'Html'},
        {SkillName:'CSS'},
        {SkillName:'SQL'},
        {SkillName:'Angular'},
        {SkillName:'Azure'}
      ];

    $scope.previousSkills = function() {
       $scope.currentPage = $scope.currentPage - 1;
    };

    $scope.nextSkills = function() {
       $scope.currentPage = $scope.currentPage + 1;
    };
}]);

app.filter('offset', function() {
  return function(input, start) {
    start = parseInt(start, 10);
    return input.slice(start);
  };
});




Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM