1

I want to have something like the following HTML code using AngularJS:

<p class="itemsperpage">Items per page: <span>20</span> <span><a href="#">40</a></span> <span><a href="#">60</a></span> <span><a href="#">View all</a></span></p>

Where 20 is wrapped only by a , because is the current active element.

Now using AngularJS, how can I have something like:

var itemsPerPage = [20,40,60];
...
<p class="itemsperpage">Items per page:
    <span ng-repeat='i in itemsPerPage'>
        // here should print {{ i }} for the current active
        // <a href="#">{{ i }}</a> for the elements not active
    </span>
    <span><a href="#">View all</a></span>
</p>

Is it possible to do something like this using AngularJS?

2 Answers 2

2

You could use ng-if like this:

<span ng-repeat='i in itemsPerPage'>
    <span ng-if="active === i">{{i}}</span>
    <a    ng-if="active !== i" href="#">{{i}}</a>
</span>

Simply keep the active item in $scope.active, for instance

$scope.active = 20;
Sign up to request clarification or add additional context in comments.

2 Comments

This works even if will add an extra <span></span> inside the <span> that contains the ng-repeat.
If you really want to get rid of that you'd need a custom directive, so a more complex code. It is better it you can accomodate with it.
1

Assuming first element would be active. Or for checking condition of 20, you could replace $first with 20

Markup

<p class="itemsperpage">Items per page:
    <span ng-repeat='i in itemsPerPage'>
        <a href="#" ng-if="$first" ng-class="{active: $first }">{{ i }}</a> 
        <span href="#" ng-if="!$first">{{ i }}</span> 
    </span>
    <span><a href="#">View all</a></span>
</p>

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.