0

I have an array like this

 $scope.mentors = [ {"name":"Jonathan", "status":0},
                    {"name": "Nathan","status":1},
                    {"name": "Chris","status":1},
                    {"name": "Brian","status":0}];

here my view code

<div ng-repeat="m in mentors">
   <div ng-if="m.status == '1'">
   <div>
     {{m.name}}
   </div>

   </div>
</div>

and my result is : Nathan and Chris

what can I do to just make it show Nathan or Chris it must only show one result only

p/s: already try ng-show="$last" it did not work since Brian status is '0'

4
  • Why do you need only one result ? Do you need to show Nathan or Chris ? Commented Nov 29, 2017 at 7:37
  • @Weedoze it can Nathan or Chris but it must only show one. if possible i want Nathan to show first until Nathan status is 0, then it will show Chris.. i hope it not too confusing Commented Nov 29, 2017 at 7:39
  • I guess you could use limitTo in your ng-repeat Commented Nov 29, 2017 at 7:40
  • @Und3rTow try it already it did not work Commented Nov 29, 2017 at 7:46

3 Answers 3

2

You can filter the array on statuses and then just take the first by checking against $index

angular.module('app', []).controller('myCtrl',function($scope){
$scope.mentors = [ {"name":"Jonathan", "status":0},
                    {"name": "Nathan","status":1},
                    {"name": "Chris","status":1},
                    {"name": "Brian","status":0}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<!DOCTYPE html>
<html>
<body ng-app="app">
<div ng-controller="myCtrl">
   <div ng-repeat="m in mentors | filter : {status: 1}">
   <div ng-if="$index == 0">
     <div>
       {{m.name}}
     </div>

   </div>
   </div>

</div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

3 Comments

This is the same code as provided. This does not provide the expected output
sorry no i only want nathan to show not chris as well.. and if nathan status turn '0' only then it will show chris
yes thank you !!... this is exactly like i wanted !! thank @MarcusHöglund
2

You could use the limitTo and filter

<div ng-repeat="m in mentors | filter: { status : '1'}| limitTo : 1">
  <div>
   {{m.name}}
  </div>
</div>

1 Comment

thank you i din't know that filter and limitTo can be used like this
0

There is no reason to use ng-repeat if you only need to display 1 item.

You should handle this in your controller. Create a new variable that will hold the mentor that you want to show.

To find Nathan, you can use Array#find(). It will return the first item that matches the condition status === 1

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.mentors = [{
      "name": "Jonathan",
      "status": 0
    },
    {
      "name": "Nathan",
      "status": 1
    },
    {
      "name": "Chris",
      "status": 1
    },
    {
      "name": "Brian",
      "status": 0
    }
  ];
  
  $scope.mentor = $scope.mentors.find(m => m.status === 1);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <h1>{{mentor.name}}</h1>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.