AngularJs ng-repeat Filter By Limit Conditionally
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
add a comment |
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
add a comment |
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
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
angularjs angularjs-ng-repeat angularjs-filter angular-ng-if ng-filter
edited Nov 19 '18 at 22:07
hmd
asked Nov 19 '18 at 21:58
hmdhmd
8301611
8301611
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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>
Thanks a lot @miqh, it looks like a smart solution. Let me try that!
– hmd
Nov 20 '18 at 9:59
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
Thanks a lot @miqh, it looks like a smart solution. Let me try that!
– hmd
Nov 20 '18 at 9:59
add a comment |
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>
Thanks a lot @miqh, it looks like a smart solution. Let me try that!
– hmd
Nov 20 '18 at 9:59
add a comment |
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>
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>
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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