-
Notifications
You must be signed in to change notification settings - Fork 26.2k
Using local variables with ngFor #6947 8000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Seconding this, I was just looking for the same thing, then searched here hoping for existing solutions. Having to have duplicate code here is silly, and being able to call a function only once (in case it's expensive) without having to first store its result in the component admittedly also adds a bunch of convenience. I'd consider this feature request a pretty valuable improvement for the template language. |
dup of #2451 ? |
Looks like it to me, yeah. Guess if @templth agrees we can close this copy. |
Sure, I agree ;-) Thanks guys for pointing this out! |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Context
Local variables (defined with the
#
character at a element attribute) don't apply for this use case: rqw local variables for a loop withngFor
.In fact, when you define a local variable on an HTML element it corresponds to the component if any. When there is no component on the element, the variable refers to the element itself.
Specifying a value for a local variable allows you to select a specific directive associated with the current element. For example:
will set the instance of the
ngForm
directive associated with the current in thename
variable.So local variables don't allow to set a value created for the current element of a loop.
If I try to do something like that:
I have this following error:
Angular2 actually looks for a directive matching the provided name
elt.title
here)... This plunkr shows this: https://plnkr.co/edit/qcMGr9FS7yQD8LbX18uY?p=preview. The section "Local variables" of this link http://victorsavkin.com/post/119943127151/angular-2-template-syntax also describes this.In addition to the current element of the iteration,
ngFor
only provides a set of exported values that can be aliased to local variables:index
,last
,even
andodd
.Question
It would be interested to have a way to define custom local variables within the loop. This way you could define a computed variable once and use it several times in the loop block.
I would be interested in having your feedbacks.
This question follows the one from the StackOverflow website: http://stackoverflow.com/questions/35257455/ng2-how-to-create-variable-in-ngfor-loop/.
Thanks very much!
The text was updated successfully, but these errors were encountered: