0

I have this code. $scope.counter = '1';

<div ng-repeat="cat in catadata">
   <div ng-if="cat.TopCategoryID = 2" >
      <div ng-if="counter = 1" >
         <div class="feedsDisplay" ng-init="counter = counter + 1">
            Creater Img:{{cat.CreatedBy_Profile.Picture.URI}}" 
         </div>
      </div>
   </div>
</div>

I want to stop loop when counter = 2, I want to display just 1 result.

ng-init="counter = counter + 1
{{counter}}

displaying 11111 it should display 12345

4
  • It's really not clear what you're trying to do. Do you really want to display only one element? And why are you doing equality checks with = instead of ==? Commented Sep 18, 2015 at 12:44
  • If you want to display only the result with index 1, just do this {{ catadata[0].CreatedBy_Profile.Picture.URI }} Commented Sep 18, 2015 at 12:46
  • Do you need quotes around the 1? Commented Sep 18, 2015 at 12:47
  • can you provide working sample on jsfiddle or plunkr? and explain want you try to do? not it is unclear Commented Sep 18, 2015 at 12:52

3 Answers 3

2

you are comparing with = instead of ==

Try like this

ng-if="counter == 1"

value of counter is string . add + sign before counter

try like this

ng-init="counter = +counter + 1"
Sign up to request clarification or add additional context in comments.

3 Comments

ng-init="counter = counter + 1 {{counter}} displaying 11111 it should display 12345
@MuhammadRiaz, first counter value is string so when you do + in just add simbol to string
like this counter = +counter + 1
2

You cant stop ng-repeat conditionally. To print only one result you can use

<div ng-repeat="cat in catadata | limitTo : 1">

OR

<div>{{catadata[0]}}</div>

3 Comments

I want limit within ng-if condition <div ng-if="cat.TopCategoryID = 2" >
@MuhammadRiaz so add filter like cat in catadata | filter: {TopCategoryID : 2}
@Riaz do it as <div ng-if="cat.TopCategoryID == 2" >. But remember ng-repeat will run for all elements of array. ng-if will only prevent the element to be created.
1

Rather than misusing ng-repeat, ng-init and ng-if like this, just try to extract the first matching cat before you render it, e.g. in a controller, directive or service:

for (var i = 0, maxI = catadata.length; i < maxI; ++i) {
    if (cat.TopCategoryID === 2) {
        $scope.firstMatchingCat = cat;
        break;
    }
}

And then in the template:

<div class="feedsDisplay">
    Creater Img:{{firstMatchingCat.CreatedBy_Profile.Picture.URI}}" 
</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.