AngularJs ng-repeat Filter By Limit Conditionally












0















How can I run the ng-repeat for maximum 2 times but each iteration should meet the condition and output the final data with two elements. For example, I have an array like:



$scope.users = [
{
id: '1',
name:'Ali',
status: true
},
{
id: '2',
name:'Wajahat',
status: false
},
{
id: '3',
name:'Hammad',
status: true
},
{
id: '4',
name:'Ahmad',
status: true
}
];


And the HTML is this:



<div ng-repeat="user in users | limitTo:2" ng-if="user.status==true">{{user.name}}</div>


The issue is, I'm getting only 1 element as an output, i.e. Ali instead of Ali and Hammad. Because ng-repeat stopped after 2nd iteration and didn't check the other elements. So, how can I get all the matching elements by status==true with the given limit?










share|improve this question





























    0















    How can I run the ng-repeat for maximum 2 times but each iteration should meet the condition and output the final data with two elements. For example, I have an array like:



    $scope.users = [
    {
    id: '1',
    name:'Ali',
    status: true
    },
    {
    id: '2',
    name:'Wajahat',
    status: false
    },
    {
    id: '3',
    name:'Hammad',
    status: true
    },
    {
    id: '4',
    name:'Ahmad',
    status: true
    }
    ];


    And the HTML is this:



    <div ng-repeat="user in users | limitTo:2" ng-if="user.status==true">{{user.name}}</div>


    The issue is, I'm getting only 1 element as an output, i.e. Ali instead of Ali and Hammad. Because ng-repeat stopped after 2nd iteration and didn't check the other elements. So, how can I get all the matching elements by status==true with the given limit?










    share|improve this question



























      0












      0








      0








      How can I run the ng-repeat for maximum 2 times but each iteration should meet the condition and output the final data with two elements. For example, I have an array like:



      $scope.users = [
      {
      id: '1',
      name:'Ali',
      status: true
      },
      {
      id: '2',
      name:'Wajahat',
      status: false
      },
      {
      id: '3',
      name:'Hammad',
      status: true
      },
      {
      id: '4',
      name:'Ahmad',
      status: true
      }
      ];


      And the HTML is this:



      <div ng-repeat="user in users | limitTo:2" ng-if="user.status==true">{{user.name}}</div>


      The issue is, I'm getting only 1 element as an output, i.e. Ali instead of Ali and Hammad. Because ng-repeat stopped after 2nd iteration and didn't check the other elements. So, how can I get all the matching elements by status==true with the given limit?










      share|improve this question
















      How can I run the ng-repeat for maximum 2 times but each iteration should meet the condition and output the final data with two elements. For example, I have an array like:



      $scope.users = [
      {
      id: '1',
      name:'Ali',
      status: true
      },
      {
      id: '2',
      name:'Wajahat',
      status: false
      },
      {
      id: '3',
      name:'Hammad',
      status: true
      },
      {
      id: '4',
      name:'Ahmad',
      status: true
      }
      ];


      And the HTML is this:



      <div ng-repeat="user in users | limitTo:2" ng-if="user.status==true">{{user.name}}</div>


      The issue is, I'm getting only 1 element as an output, i.e. Ali instead of Ali and Hammad. Because ng-repeat stopped after 2nd iteration and didn't check the other elements. So, how can I get all the matching elements by status==true with the given limit?







      angularjs angularjs-ng-repeat angularjs-filter angular-ng-if ng-filter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 22:07







      hmd

















      asked Nov 19 '18 at 21:58









      hmdhmd

      8301611




      8301611
























          1 Answer
          1






          active

          oldest

          votes


















          4














          You can chain filter onto your ng-repeat expression to apply the conditional filtering you need first before limitTo kicks in.



          Note that the ordering of expressions is significant in this approach.






          angular
          .module('app', )
          .controller('ctrl', function ($scope) {
          $scope.users = [
          {
          id: '1',
          name:'Ali',
          status: true,
          },
          {
          id: '2',
          name:'Wajahat',
          status: false,
          },
          {
          id: '3',
          name:'Hammad',
          status: true,
          },
          {
          id: '4',
          name:'Ahmad',
          status: true,
          }
          ];

          });

          <div ng-app="app" ng-controller="ctrl">
          <div ng-repeat="user in users | filter:{status: true} | limitTo:2">{{ user.name }}</div>
          </div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>








          share|improve this answer
























          • Thanks a lot @miqh, it looks like a smart solution. Let me try that!

            – hmd
            Nov 20 '18 at 9:59











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383231%2fangularjs-ng-repeat-filter-by-limit-conditionally%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You can chain filter onto your ng-repeat expression to apply the conditional filtering you need first before limitTo kicks in.



          Note that the ordering of expressions is significant in this approach.






          angular
          .module('app', )
          .controller('ctrl', function ($scope) {
          $scope.users = [
          {
          id: '1',
          name:'Ali',
          status: true,
          },
          {
          id: '2',
          name:'Wajahat',
          status: false,
          },
          {
          id: '3',
          name:'Hammad',
          status: true,
          },
          {
          id: '4',
          name:'Ahmad',
          status: true,
          }
          ];

          });

          <div ng-app="app" ng-controller="ctrl">
          <div ng-repeat="user in users | filter:{status: true} | limitTo:2">{{ user.name }}</div>
          </div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>








          share|improve this answer
























          • Thanks a lot @miqh, it looks like a smart solution. Let me try that!

            – hmd
            Nov 20 '18 at 9:59
















          4














          You can chain filter onto your ng-repeat expression to apply the conditional filtering you need first before limitTo kicks in.



          Note that the ordering of expressions is significant in this approach.






          angular
          .module('app', )
          .controller('ctrl', function ($scope) {
          $scope.users = [
          {
          id: '1',
          name:'Ali',
          status: true,
          },
          {
          id: '2',
          name:'Wajahat',
          status: false,
          },
          {
          id: '3',
          name:'Hammad',
          status: true,
          },
          {
          id: '4',
          name:'Ahmad',
          status: true,
          }
          ];

          });

          <div ng-app="app" ng-controller="ctrl">
          <div ng-repeat="user in users | filter:{status: true} | limitTo:2">{{ user.name }}</div>
          </div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>








          share|improve this answer
























          • Thanks a lot @miqh, it looks like a smart solution. Let me try that!

            – hmd
            Nov 20 '18 at 9:59














          4












          4








          4







          You can chain filter onto your ng-repeat expression to apply the conditional filtering you need first before limitTo kicks in.



          Note that the ordering of expressions is significant in this approach.






          angular
          .module('app', )
          .controller('ctrl', function ($scope) {
          $scope.users = [
          {
          id: '1',
          name:'Ali',
          status: true,
          },
          {
          id: '2',
          name:'Wajahat',
          status: false,
          },
          {
          id: '3',
          name:'Hammad',
          status: true,
          },
          {
          id: '4',
          name:'Ahmad',
          status: true,
          }
          ];

          });

          <div ng-app="app" ng-controller="ctrl">
          <div ng-repeat="user in users | filter:{status: true} | limitTo:2">{{ user.name }}</div>
          </div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>








          share|improve this answer













          You can chain filter onto your ng-repeat expression to apply the conditional filtering you need first before limitTo kicks in.



          Note that the ordering of expressions is significant in this approach.






          angular
          .module('app', )
          .controller('ctrl', function ($scope) {
          $scope.users = [
          {
          id: '1',
          name:'Ali',
          status: true,
          },
          {
          id: '2',
          name:'Wajahat',
          status: false,
          },
          {
          id: '3',
          name:'Hammad',
          status: true,
          },
          {
          id: '4',
          name:'Ahmad',
          status: true,
          }
          ];

          });

          <div ng-app="app" ng-controller="ctrl">
          <div ng-repeat="user in users | filter:{status: true} | limitTo:2">{{ user.name }}</div>
          </div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>








          angular
          .module('app', )
          .controller('ctrl', function ($scope) {
          $scope.users = [
          {
          id: '1',
          name:'Ali',
          status: true,
          },
          {
          id: '2',
          name:'Wajahat',
          status: false,
          },
          {
          id: '3',
          name:'Hammad',
          status: true,
          },
          {
          id: '4',
          name:'Ahmad',
          status: true,
          }
          ];

          });

          <div ng-app="app" ng-controller="ctrl">
          <div ng-repeat="user in users | filter:{status: true} | limitTo:2">{{ user.name }}</div>
          </div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>





          angular
          .module('app', )
          .controller('ctrl', function ($scope) {
          $scope.users = [
          {
          id: '1',
          name:'Ali',
          status: true,
          },
          {
          id: '2',
          name:'Wajahat',
          status: false,
          },
          {
          id: '3',
          name:'Hammad',
          status: true,
          },
          {
          id: '4',
          name:'Ahmad',
          status: true,
          }
          ];

          });

          <div ng-app="app" ng-controller="ctrl">
          <div ng-repeat="user in users | filter:{status: true} | limitTo:2">{{ user.name }}</div>
          </div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 22:16









          miqhmiqh

          2,68021930




          2,68021930













          • Thanks a lot @miqh, it looks like a smart solution. Let me try that!

            – hmd
            Nov 20 '18 at 9:59



















          • Thanks a lot @miqh, it looks like a smart solution. Let me try that!

            – hmd
            Nov 20 '18 at 9:59

















          Thanks a lot @miqh, it looks like a smart solution. Let me try that!

          – hmd
          Nov 20 '18 at 9:59





          Thanks a lot @miqh, it looks like a smart solution. Let me try that!

          – hmd
          Nov 20 '18 at 9:59




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383231%2fangularjs-ng-repeat-filter-by-limit-conditionally%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)