Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ng-repeat should accept strings #8870

Closed
ilanbiala opened this issue Aug 31, 2014 · 11 comments
Closed

ng-repeat should accept strings #8870

ilanbiala opened this issue Aug 31, 2014 · 11 comments
Milestone

Comments

@ilanbiala
Copy link

If I have a blog and a pagination widget at the bottom, I get a response from the server like this:

$scope.response = {
  articles: [],
  page: 3
}

It would be very simple to do something like this:

<ul class="pagination">
  <li ng-repeat="response.page" ng-class="{ 'current': response.page === $index + 1 }"><a>{{$index + 1}}</a></li>
</ul>

I can't really think of a better way to handle a situation like this where I know how many ng-repeat iterations have to happen, and the data can be used from the data provided by ng-repeat itself. If there is a better solution, I'm all for it, but I think ng-repeat offers simplicity and ease of use.

@caitp
Copy link
Contributor

caitp commented Aug 31, 2014

that markup doesn't really make any sense --- what do you want to repeat over?

@gkalpak
Copy link
Member

gkalpak commented Sep 1, 2014

@caitp: I believe they just want to create response.page items and they don't need any extra data other than what ngRepeat creates (i.e. $index/$first/$last etc). Basically, it would be a shortcut to creating an array containing the values 0, 1, 2 (since response.page === 3).

@caitp
Copy link
Contributor

caitp commented Sep 1, 2014

That doesn't really make any sense, and would not end up doing what they want it to do. It sounds like another "make life easier" pill but would end up not being all that useful because you still wouldn't be specifying a range to iterate over

@gkalpak
Copy link
Member

gkalpak commented Sep 1, 2014

I think it does make sense, but for very specific and limited cases.
Considering that (and the fact that it is so easy to implement yourself (e.g. using a $scope function or even a filter)), I don't see any reason this should be implemented in Angular.

@ilanbiala
Copy link
Author

@caitp the range would be from 0 to response.page - 1, just like with an array.

@gkalpak but implementing it myself isn't as clean and the function really serves a small purpose.

@caitp if I could come up with a few more use cases, would you be more open to the idea?

@lgalfaso
Copy link
Contributor

lgalfaso commented Sep 1, 2014

it does look a lot like #8210

@caitp
Copy link
Contributor

caitp commented Sep 1, 2014

@lgalfaso the range operator is a better idea because it's more explicit about what you want. I don't think implicitly walking for each number between 0 and the target number is intuitive or useful to pretty much anyone, including this very use case. That is not how I would implement a pagination control

@pkozlowski-opensource
Copy link
Member

I'm with @lgalfaso and @caitp - this is a perfect use-case for the range operator - which btw I would love to have in AngularJS.

@ilanbiala do you have other use-cases on your mind that could not be solved with the range operator proposal from @lgalfaso ? If not I would suggest that we focus on proper implementation of the mentioned range.

@pkozlowski-opensource pkozlowski-opensource added this to the Purgatory milestone Sep 1, 2014
@ilanbiala
Copy link
Author

@pkozlowski-opensource I think that the range operator would solve this and a couple other use cases. I'm all for implementing that, because it seems to do some other nice things as well. :)

@gkalpak
Copy link
Member

gkalpak commented Sep 2, 2014

How about a toRange filter. It should be the easiest to implement (no existing functionality needs to be modified, only something to be added). I could take an array of two elements as input (start-, end-index) and an optional step and return an array of integers ranging from start-index to end-index (with the specified step). It would have sensible defaults (e.g. start-index: 0, step: 1 if omitted etc) and should be easily extensible to support other categories such as floats, letters etc.

E.g. something like this:

ng-repeat="x in ([1, 5] | toRange)" <=> ng-repeat="x in [1, 2, 3, 4, 5]"

ng-repeat="x in ([0, 9] | toRange:2)" <=> ng-repeat="x in [0, 2, 4, 6, 8]"

@tbosch
Copy link
Contributor

tbosch commented Sep 2, 2014

Closing this in favor of #8210.

@tbosch tbosch closed this as completed Sep 2, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants