0

I have an ng-repeat which repeats two <li> s a particular number of time. I am also using the $index to see which index the outputted <li> is.

A sample is on this fiddle: http://jsfiddle.net/sh0ber/cHQLH

However, I need that one of the <li>s when using the index rather than outputting numbers like 1, 2, 3 etc, it outputs letters like A, B, C and so on.

Is there a way to do this in AngularJS? I am new to this.

EDIT:

I need that the output would be in the following way: C1, C2, C3.... B1, B2, B3.... A1, A2, A3...

i.e. the letters are descending.

Thanks

2 Answers 2

5

You can write a filter:

.filter('character',function(){
    return function(input){
        return String.fromCharCode(64 + parseInt(input,10));
    };
});

and use it:

{{$index+1|character}}

DEMO

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

3 Comments

thanks for the demo. Is there a way to concatenate the indexes together so I get something like A1, A2, A3.... B1, B2, B3.... C1, C2, C3... I have updated the fiddle (jsfiddle.net/65FMq/1) but for some reason cannot get them in the above format, but rather A1, B2, C3... A1, B2, C3... A1, B2, C3... ...
@user1809790 is this what you want? I have changed $index+1 to $parent.$index+1.
how easy is it to change the order of the index to obtain something like: C1, C2, C3 B1, B2, B3 A1, A2, A3 (i.e. change the order of the letters from ascending to descending)
1

This isn't really angular specific but you could easily define a function that converts from the number (index) to the letter that you want. I am assuming ASCII characters here and defined the following function:

$scope.getLetter = function (index) {
    return String.fromCharCode(65 + index);
}

To print this out, then all you need to do is:

<li ng-repeat="i in getNumber(myOtherNumber)"><span>{{getLetter($index)}}</span></li>

To see this all working, take a look at this fiddle: http://jsfiddle.net/vd4Z4/

1 Comment

I have updated the fiddle (jsfiddle.net/vd4Z4/1). Basically what I need to do is give each cell a combination of letter and number. I want the cells to have IDs in the following fashion: A1, A2, A3.... B1, B2, B3.... C1, C2, C3... At the moment I am getting A1, B2, C3 A1, B2, C3 A1, B2, C3 Is it possible to make them in the format above?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.